在android中创建SQLite数据库 [英] Create SQLite database in android

查看:144
本文介绍了在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());
   }

这篇关于在android中创建SQLite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆