Android 中的 SQLite 数据库中有多个表吗?

发布于 2024-11-01 09:22:38 字数 1798 浏览 0 评论 0原文

我在 android 的 sqllite 数据库中创建多个表时遇到问题。 这是我的代码

   public class DataHelper {
   private static final String DATABASE_NAME = "example.db";
   private static final int DATABASE_VERSION = 1;
   private static final String TABLE_NAME = "table1";
   private static final String SETTING_TABLE="setting";

   private Context context;
   private SQLiteDatabase db;

   private SQLiteStatement insertStmt;
   private static final String INSERT = "insert into " + TABLE_NAME + 
       "(name) values (?)";
   private static final String INSERTSETTING = "insert into " + SETTING_TABLE +
       "(name) values (?)";

   public DataHelper(Context context) {
      this.context = context;
      OpenHelper openHelper = new OpenHelper(this.context);
      this.db = openHelper.getWritableDatabase();
      this.insertStmt = this.db.compileStatement(INSERT);
      this.insertStmt = this.db.compileStatement(INSERTSETTING);
   }

   public SQLiteDatabase getDb() {
      return this.db;
   }

   public long insert(String name) {
      this.insertStmt.bindString(1, name);
      return this.insertStmt.executeInsert();
   }   
   private static class OpenHelper extends SQLiteOpenHelper {
      OpenHelper(Context context) {
         super(context, DATABASE_NAME, null, DATABASE_VERSION);
      }
      @Override
      public void onCreate(SQLiteDatabase db) {
         db.execSQL("CREATE TABLE " + TABLE_NAME + 
              " (id INTEGER PRIMARY KEY, name TEXT)");
         db.execSQL("CREATE TABLE " + SETTING_TABLE + 
              " (id INTEGER PRIMARY KEY, name TEXT)");
      }
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
      {
         db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
         onCreate(db);
      }
   }
}

I am facing problem in creating more than one tables in sqllite database in android.
Here is my code

   public class DataHelper {
   private static final String DATABASE_NAME = "example.db";
   private static final int DATABASE_VERSION = 1;
   private static final String TABLE_NAME = "table1";
   private static final String SETTING_TABLE="setting";

   private Context context;
   private SQLiteDatabase db;

   private SQLiteStatement insertStmt;
   private static final String INSERT = "insert into " + TABLE_NAME + 
       "(name) values (?)";
   private static final String INSERTSETTING = "insert into " + SETTING_TABLE +
       "(name) values (?)";

   public DataHelper(Context context) {
      this.context = context;
      OpenHelper openHelper = new OpenHelper(this.context);
      this.db = openHelper.getWritableDatabase();
      this.insertStmt = this.db.compileStatement(INSERT);
      this.insertStmt = this.db.compileStatement(INSERTSETTING);
   }

   public SQLiteDatabase getDb() {
      return this.db;
   }

   public long insert(String name) {
      this.insertStmt.bindString(1, name);
      return this.insertStmt.executeInsert();
   }   
   private static class OpenHelper extends SQLiteOpenHelper {
      OpenHelper(Context context) {
         super(context, DATABASE_NAME, null, DATABASE_VERSION);
      }
      @Override
      public void onCreate(SQLiteDatabase db) {
         db.execSQL("CREATE TABLE " + TABLE_NAME + 
              " (id INTEGER PRIMARY KEY, name TEXT)");
         db.execSQL("CREATE TABLE " + SETTING_TABLE + 
              " (id INTEGER PRIMARY KEY, name TEXT)");
      }
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
      {
         db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
         onCreate(db);
      }
   }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

用心笑 2024-11-08 09:22:38

增加 DATABASE_VERSION 的值,否则 onUpgrade 将不会被调用。

我注意到您没有在 onUpgade 中删除 SETTING_TABLE,并且当您将 INSERT 替换为 INSERTSETTING 时,您会丢失代码顶部的第一个插入语句。

Increase the value of DATABASE_VERSION, otherwise onUpgrade won't get called.

I notice you don't drop the SETTING_TABLE in onUpgade, and you're losing the first insert statement at the top of your code, as you replace INSERT with INSERTSETTING.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文