Create SQLite database in android


I want to create a SQLite database in my app, which contains three tables, I will add data into tables and will use them later on.

but I like to keep database ,as if when app is first time installed it checks whether the database exist or not, if exists it updates it else if not then creates a new database.

further more I am making a DB class to facilitate my app,so I wont be creating an activity for my database creation.

if there are possible advices, please share with me

7/21/2010 9:46:22 AM

Accepted Answer

Better example is [here]

 try {
   myDB = this.openOrCreateDatabase("DatabaseName", MODE_PRIVATE, null);

   /* Create a Table in the Database. */
     + TableName
     + " (Field1 VARCHAR, Field2 INT(3));");

   /* Insert data to a Table*/
   myDB.execSQL("INSERT INTO "
     + TableName
     + " (Field1, Field2)"
     + " VALUES ('Saranga', 22);");

   /*retrieve data from database */
   Cursor c = myDB.rawQuery("SELECT * FROM " + TableName , null);

   int Column1 = c.getColumnIndex("Field1");
   int Column2 = c.getColumnIndex("Field2");

   // Check if our result was valid.
   if (c != null) {
    // Loop through all Results
    do {
     String Name = c.getString(Column1);
     int Age = c.getInt(Column2);
     Data =Data +Name+"/"+Age+"\n";
5/5/2017 6:52:31 AM

If you want to keep the database between uninstalls you have to put it on the SD Card. This is the only place that won't be deleted at the moment your app is deleted. But in return it can be deleted by the user every time.

If you put your DB on the SD Card you can't use the SQLiteOpenHelper anymore, but you can use the source and the architecture of this class to get some ideas on how to implement the creation, updating and opening of a databse.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow