SQLite数据库中的第二个表未创建

Dyl*_*erg 0 database sqlite android multiple-tables sqliteopenhelper

我知道以前有很多关于这个主题的问题,但这些问题/答案都没有帮助.我正在创建一个Android应用程序,我有一个包含多个表的数据库.第一个成功创建,但第二个从未创建.当我稍后尝试访问该表时,它给出了"表不存在"错误.我曾多次尝试更改数据库名称,更改数据库版本,清除缓存和数据,卸载并重新安装我的应用程序.什么都行不通.我使用sqlite浏览器查看我的数据库,第二个表不存在.我已经尝试在增加版本之后onCreate在我的方法中添加断点SQLiteDatabaseOpenHelper.调试器直接通过.以下是适用的代码:First Table 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
}
Run Code Online (Sandbox Code Playgroud)

这是我的第二个表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
}
Run Code Online (Sandbox Code Playgroud)

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

SessionLeadersDatabaseOpenHelper sldb = new SessionLeadersDatabaseOpenHelper(applicationContext);
Run Code Online (Sandbox Code Playgroud)

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

SessionDescriptionsDatabaseOpenHelper sddb = new SessionDescriptionsDatabaseOpenHelper(applicationContext);
Run Code Online (Sandbox Code Playgroud)

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

Jam*_*mil 6

在一个单独的帮助器类中创建两个表

试试这个=

    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
    }
Run Code Online (Sandbox Code Playgroud)

编辑:我已经编辑了代码