Create database and insert records in it for Android

Create a Database

Simple steps to create a database and handle are as following.
  1. Create "SQLiteDatabase" object.
  2. Open or Create database and create connection.
  3. Perform insert, update or delete operation.
  4. Create Cursor to display data from table of database.
  5. Close the database connectivity.
Following tutorial helps you to create database and insert records in it.

Step 1:
 Instantiate "SQLiteDatabase" objectSQLiteDatabase db;
Before you can use the above object, you must import the android.database.sqlite.SQLiteDatabasenamespace in your application. 
db=openOrCreateDatabase(String path, int mode, SQLiteDatabase.CursorFactory factory) 
This method is used to create/open database. As the name suggests, it will open a database connection if it is already there, otherwise it will create a new one.



Step 2: Execute DDL command

db.execSQL(String sql) throws SQLException

This command is used to execute single SQL statement which doesn't return any data means other than SELECT or any other.

db.execSQL("Create Table Temp (id Integer, name Text)");

In the above example, it takes "CREATE TABLE" statement of SQL. This will create a table of "Integer" & "Text" fields.

Try and Catch block is require while performing this operation. An exception that indicates there was an error with SQL parsing or execution.

Step 3:
 Create object of "ContentValues" and Initiate it.ContentValues values=new ContentValues();

This class is used to store a set of values. We can also say, it will map ColumnName and relavent ColumnValue.
values.put("id", eid.getText().toString());          
"name", ename.getText().toString()); 
String Key
Name of field as in table. Ex. "id", "name"
String Value
Value to be inserted.
Step 4: Perform Insert Statement.insert(String table, String nullColumnHack, ContentValues values)
String table
Name of table related to database.
String nullColumnHack
If not set to null, the nullColumnHack parameter provides the name of nullable column name to explicitly insert a NULL into in the case where yourvalues is empty.
ContentValues values
This map contains the initial column values for the row.
This method returns a long. The row ID of the newly inserted row, or -1 if an error occurred.

db.insert("temp", null, values);

Step 5:
 Create Cursor

This interface provides random read-write access to the result set returned by a database query.
Cursor c=db.rawQuery(String sql, String[] selectionArgs)

Strign sql
The SQL query
String []selectionArgs
You may include ?s in where clause in the query, which will be replaced by the values from selectionArgs. The values will be bound as Strings.
Example,Cursor c=db.rawQuery("SELECT * FROM temp",null);

Moves cursor pointer at first position of result set
Moves cursor pointer next to current position.
Returs false, if cursor pointer is not at last position of result set.


Step 6:
 Close Cursor and Close Database connectivity

It is very important to release our connections before closing our activity. It is advisable to release the Database connectivity in "onStop" method. And Cursor connectivity after use it.


Related Posts Plugin for WordPress, Blogger...