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

时间:2014-08-08 18:49:32

标签: android database sqlite 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
}

这是我的第二张表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);

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

1 个答案:

答案 0 :(得分: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
    }

编辑:我已编辑代码