创建于Android的SQLite数据库

创建于Android的SQLite数据库

问题描述:

我想在我的应用程序,其中包含三个表创建一个SQLite数据库,我会添加数据到表中,并使用他们稍后。

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.

进一步我想提出一个DB类,以方便我的应用程序,所以我不会要创建我的数据库创建活动。

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

很好的例子是这里

 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());
   }