Posts Tagged ‘database’

MongoDb Introduction

April 7, 2016 Leave a comment

MongoDB Databse-
MongoDB is NoSql and document oriented database. MongoDB stores data using a flexible document data model that is similar to JSON.

Create Database-
>use yaju;

Show Database-
>show dbs

Drop Database-
To drop database, first use that database then use following command.

Create Collection-
Collections are like table in MySql. We can create collection in two ways.

First, we can create empty collection

Second, we can create dynamic collection with insert command. Mongo check that collection if collection is exists then insert data in existing collection otherwise create new collection and insert data. That can be through followning command.

Show collections-
Show all collection in a database use following command.
>show collections;

Insert document-
We can insert single document and multiple document as well.

Single document
Multiple document
>db.testtable.insert([{‘name’:’yajuvendra’},{‘name’:’yaj’}, {‘name’:’yaju’},{‘name’:’yajkumar’}]);

Select document-
Select all document
Select particular document
> db.testtable.find({‘name’:’yaj’});

Remove document-
> db.testtable.remove({‘name’:’yaju’});

Update document-
> db.testtable.update({name:’yaj’},{$set:{name:’yaju_update’}})

How to use database in Android

March 15, 2012 3 comments

Android supports SQLite database. In this example we will see these following
1. How to create or open database.
2. How to create table in database.
3. How to insert data in database table.
4. How to fetch data from database table and show.

I tested this example with ‘Andorid-2.2, sdk version-8’.
Packege Name:
Activity Name: DbActivity
File Name:

Code starts here

package;//Packeage name

import android.os.Bundle;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.widget.TextView;

public class DbActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
SQLiteDatabase myDB= null;
String TableName = “Profile”;
String ShowData=””;
/* This function create new database if not exists. */
try {
myDB = this.openOrCreateDatabase(“testdb”, MODE_PRIVATE, null);
/* Create a Table in the Database. */
+ TableName
+ ” (id INT(4),firstname VARCHAR,lastname VARCHAR);”);
/* Insert data to a Table*/
+ TableName
+ ” (id, firstname, lastname)”
+ ” VALUES (1, ‘Yaju’, ‘Kumar’);”);

/*Fetch data from database table */
Cursor c = myDB.rawQuery(“SELECT * FROM ” + TableName , null);

int id = c.getColumnIndex(“id”);
int fristName = c.getColumnIndex(“firstname”);
int lastName = c.getColumnIndex(“lastname”);

// Check result.
if (c != null) {
// Loop through all Results
do {
int personId = c.getInt(id);
String FirstName = c.getString(fristName);
String LastName = c.getString(lastName);
ShowData =ShowData +personId+” .) “+FirstName+” “+LastName+”\n”;
TextView tv = new TextView(this);
catch(Exception e) {
Log.e(“Error”, “Error”, e);
} finally {
if (myDB != null)


%d bloggers like this: