Posts

Showing posts from April, 2009

Database Creation in Android

Steps for creating a database , creating table into it, inserting value and fetching the values and displaying in a listActivity

Step-I

First of all, create an object of a database, using,

SQLiteDatabase myDB = null;
myDB = databaseAct.this.openOrCreateDatabase("MyDatabase",
MODE_PRIVATE, null);



Here, "MyDatabase" is the name of database. MODE_PRIVATE is file creation mode. In this mode, only created file can be accessed by the calling application or all application sharing the same userID.

Step - II

Next step is, creating a table in the database. Here, our table name is "MyTable". The command is,

myDB.execSQL("CREATE TABLE IF NOT EXISTS MyTable (lastName varchar, "
+ "firstName varchar, age INT(3));");

Now, we created a table with three fields such as lastName, FirstName and Age.

Step - III

Inserting values into the table using the function, execSQL() executes a SQL statement, not a query

myDB.execSQL("INSERT INTO MyTable VALUES ('Guna&#…