【问题标题】:Second Table in SQLite Database is not being createdSQLite 数据库中的第二个表未创建
【发布时间】:2014-08-08 18:49:31
【问题描述】:

我知道以前有人就这个话题提出过很多问题,但这些问题/答案都没有帮助。我正在创建一个 android 应用程序,并且我有一个包含多个表的数据库。第一个创建成功,但第二个从未创建。当我稍后尝试访问该表时,它给了我一个“表不存在”错误。我已经尝试过无数次更改数据库名称、更改数据库版本、清除缓存和数据、卸载并重新安装我的应用程序。没有任何效果。我使用 sqlite 浏览器查看我的数据库,第二个表不存在。增加版本后,我尝试在我的SQLiteDatabaseOpenHelperonCreate 方法中放置断点。调试器直接通过。这是适用的代码: 第一个表 DBHelper:

public class SessionDescriptionsDatabaseOpenHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "database.db";
private static final String TABLE_SESSIONDESCRIPTIONS = "session_descriptions";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_TITLE = "title";
private static final String COLUMN_DESCRIPTION = "description";

public SessionDescriptionsDatabaseOpenHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {


    // Database creation SQL statement
    final String DATABASE_CREATE = "create table "
            + TABLE_SESSIONDESCRIPTIONS
            + "("
            + COLUMN_ID + " integer primary key autoincrement, "
            + COLUMN_TITLE + " text not null, "
            + COLUMN_DESCRIPTION + " text not null"
            + ");";
    db.execSQL(DATABASE_CREATE);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_SESSIONDESCRIPTIONS);

    // Create tables again
    onCreate(db);
}//more non-applicable code goes here
}

这是我的第二张表 DBHelper:

public class SessionLeadersDatabaseOpenHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "database.db";
private static final String TABLE_SESSIONLEADERS = "session_leaders";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_NAME = "category";
private static final String COLUMN_ORGANIZATION = "summary";
private static final String COLUMN_TITLE = "title";
private static final String COLUMN_DESCRIPTION = "description";
private static final String COLUMN_PICURL = "google";
public SessionLeadersDatabaseOpenHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {


    // Database creation SQL statement
    final String DATABASE_CREATE = "create table "
            + TABLE_SESSIONLEADERS
            + "("
            + COLUMN_ID + " integer primary key autoincrement, "
            + COLUMN_NAME + " text not null, "
            + COLUMN_ORGANIZATION + " text not null,"
            + COLUMN_TITLE + " text not null,"
            + COLUMN_DESCRIPTION + " text not null, "
            + COLUMN_PICURL + " text not null"
            + ");";
    db.execSQL(DATABASE_CREATE);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_SESSIONLEADERS);

    // Create tables again
    onCreate(db);
}//more non-applicable code goes here
}

这是我对第一个表的调用(在 AsyncTask 的后台线程中):

SessionLeadersDatabaseOpenHelper sldb = new SessionLeadersDatabaseOpenHelper(applicationContext);

第二个(在 AsyncTask 的后台线程中):

SessionDescriptionsDatabaseOpenHelper sddb = new SessionDescriptionsDatabaseOpenHelper(applicationContext);

我可以让它工作的唯一方法是为每个表设置一个单独的数据库,但这是非常非常糟糕的设计,并且占用了设备上不需要的空间。我执行了用于在 sqlite 浏览器中创建第二个表的 sql 语句,它运行良好。我搜索了许多教程和 stackoverflow 问题,但没有一个有帮助。我已经坚持了三天。非常感谢您的帮助。提前致谢。

【问题讨论】:

  • 您不会像下面的答案中提到的那样在单个帮助程序类中创建两个表

标签: android database sqlite multiple-tables sqliteopenhelper


【解决方案1】:

在一个辅助类中创建两个表

试试这个=

    public class SessionDescriptionsDatabaseOpenHelper extends SQLiteOpenHelper {

    // Fields for first table
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "database.db";
    private static final String TABLE_SESSIONDESCRIPTIONS = "session_descriptions";
    private static final String COLUMN_ID = "_id";
    private static final String COLUMN_TITLE = "title";
    private static final String COLUMN_DESCRIPTION = "description";

    // Fields for second table
    private static final String TABLE_SESSIONLEADERS = "session_leaders";
    private static final String COLUMN_NAME = "category";
    private static final String COLUMN_ORGANIZATION = "summary";
    private static final String COLUMN_PICURL = "google";





    public SessionDescriptionsDatabaseOpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }


  public static final String DATABASE_CREATE_TABLE1 = "create table "
                    + TABLE_SESSIONDESCRIPTIONS
                    + "("
                    + COLUMN_ID + " integer primary key autoincrement, "
                    + COLUMN_TITLE + " text not null, "
                    + COLUMN_DESCRIPTION + " text not null"
                    + ");";

       public static final String DATABASE_CREATE_TABLE2 = "create table "
                + TABLE_SESSIONLEADERS
                + "("
                + COLUMN_ID + " integer primary key autoincrement, "
                + COLUMN_NAME + " text not null, "
                + COLUMN_ORGANIZATION + " text not null,"
                + COLUMN_TITLE + " text not null,"
                + COLUMN_DESCRIPTION + " text not null, "
                + COLUMN_PICURL + " text not null"
                + ");";

    @Override
    public void onCreate(SQLiteDatabase db) {


        // Database creation SQL statement

        db.execSQL(DATABASE_CREATE_TABLE1);
        db.execSQL(DATABASE_CREATE_TABLE2);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_SESSIONDESCRIPTIONS);

        // Create tables again
        onCreate(db);
    }//more non-applicable code goes here
    }

编辑:我已经编辑了代码

【讨论】:

  • 成功了!非常感谢。现在我想一想,为什么我的代码不能工作是有道理的,因为它调用 onCreate 来创建数据库,而不是表。我遵循的教程告诉我将每个表放在不同的类中,但这是 100% 错误的。
猜你喜欢
  • 1970-01-01
  • 2014-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-18
相关资源
最近更新 更多