views:

157

answers:

3

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

A: 

Why not refer to the documentation or the sample code shipping with the SDK? There's code in the samples on how to create/update/fill/read databases using the helper class described in the document I linked.

Thorsten Dittmar
That is only a rtfm answer. This is not very helpful. Giving the correct links is important but also give some hints or a summary that shows you have done some work yourself rather then punched the question into google without thinking. Stackoverflow is the first access points for software questions for many developers and the hope to find a short answer here that may satisfy there needs before they dive into the documentation.
Janusz
Sorry, but I disagree with you here. I think it is perfectly valid to point the OP to the documentation and sample code, as there he will find much more comprehensive examples than anybody could and should post here on SO. Also, I did not just point him to the Android docs, but I pointed him specifically to the database development docs. I will, however, next time spend a second thought before I give a generic RTFM reply - but in cases where I consider this to be the right answer, I will not refrain from giving an RTFM reply :-)
Thorsten Dittmar
A: 

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.

Janusz
A: 

Nice example is here.

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

   /* Create a Table in the Database. */
   myDB.execSQL("CREATE TABLE IF NOT EXISTS "
     + 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.
   c.moveToFirst();
   if (c != null) {
    // Loop through all Results
    do {
     String Name = c.getString(Column1);
     int Age = c.getInt(Column2);
     Data =Data +Name+"/"+Age+"\n";
    }while(c.moveToNext());
   }
Paniyar