创建于Android的SQLite数据库 [英] Create SQLite database in android

查看:116
本文介绍了创建于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天全站免登陆