Java – how to add a second table to the database in SQLite?

I have a database exam guide. I have created a table_ Subject and now

private static final String DATABASE_CREATE = "create table IF NOT EXISTS "
        + TABLE_SUBJECT + "( " + COLUMN_ID
        + " integer primary key autoincrement," 
        + COLUMN_SUBJECT + " text not null,"
        + COLUMN_CHAPTER + " text,"
        + COLUMN_QUESTION + " text not null,"
        + COLUMN_OPTIONA + " text not null,"
        + COLUMN_OPTIONB + " text not null,"
        + COLUMN_OPTIONC + " text not null,"
        + COLUMN_OPTIOND + " text not null,"
        + COLUMN_CORRECT + " text not null,"
        + COLUMN_CONFIRM + " text not null);";

    private static final String DATABASE_CREATE1 = "create table IF NOT EXISTS "
    + TABLE_CHAPTER + "( " + COLUMN_ID
    + " integer primary key autoincrement," 
    + COLUMN_SUBJECT + " text not null,"
    + COLUMN_CHAPTER + " text,"
    + COLUMN_QUESTION + " text not null,"
    + COLUMN_OPTIONA + " text not null,"
    + COLUMN_OPTIONB + " text not null,"
    + COLUMN_OPTIONC + " text not null,"
    + COLUMN_OPTIOND + " text not null,"
    + COLUMN_CORRECT + " text not null,"
    + COLUMN_CONFIRM + " text not null);";

public MysqLiteHelper open() throws sqlException 
{
    db = this.getWritableDatabase();
    return this;
}

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

}


@Override
public void onCreate(sqliteDatabase database) {

    database.execsql(DATABASE_CREATE);
    database.execsql(DATABASE_CREATE1);

}

This code does not create a second table I want to use these two tables in my database It displays the following error in logcat

03-21 18:31:06.551: ERROR/Database(8255): Error inserting chapter=paging correctoption=shadow copy craete a duplicate copy of page subject=operating system question=what is shadow copy? optiona=shadow copy craete a duplicate copy of page confirm=YES optionb=sahdow copy create paging  optionc=shadow copy delete duplicate page optiond=shadow copy delete original and create shadow copy
03-21 18:31:06.551: ERROR/Database(8255): android.database.sqlite.sqliteException: no such table: chapter:,while compiling: INSERT INTO chapter(chapter,correctoption,subject,question,optiona,confirm,optionb,optionc,optiond) VALUES(?,?,?);
03-21 18:31:06.551: ERROR/Database(8255):     at android.database.sqlite.sqliteCompiledsql.native_compile(Native Method)
03-21 18:31:06.551: ERROR/Database(8255):     at android.database.sqlite.sqliteCompiledsql.compile(sqliteCompiledsql.java:92)
03-21 18:31:06.551: ERROR/Database(8255):     at   android.database.sqlite.sqliteCompiledsql.<init>(sqliteCompiledsql.java:65)
03-21 18:31:06.551: ERROR/Database(8255):     at android.database.sqlite.sqliteProgram.<init>(sqliteProgram.java:83)
03-21 18:31:06.551: ERROR/Database(8255):     at android.database.sqlite.sqliteStatement.<init>(sqliteStatement.java:41)
03-21 18:31:06.551: ERROR/Database(8255):     at android.database.sqlite.sqliteDatabase.compileStatement(sqliteDatabase.java:1149)
03-21 18:31:06.551: ERROR/Database(8255):     at android.database.sqlite.sqliteDatabase.insertWithOnConflict(sqliteDatabase.java:1569)
03-21 18:31:06.551: ERROR/Database(8255):     at android.database.sqlite.sqliteDatabase.insert(sqliteDatabase.java:1426)
03-21 18:31:06.551: ERROR/Database(8255):     at com.example.examguide.MysqLiteHelper.insertChapterData(MysqLiteHelper.java:212)
03-21 18:31:06.551: ERROR/Database(8255):     at com.example.examguide.ObjectiveAddActivity$2.onClick(ObjectiveAddActivity.java:155)

Solution

Create another create table string, and then call execsql again in oncreate:

database.execsql(DATABASE_CREATE1);
database.execsql(DATABASE_CREATE2);

edit

To add another table to an existing database, modify the onupgrade method as follows Onupgrade will be called whenever the database needs to be upgraded; Please note that you must increase version_ Number (you want to include it in the private instance variable in the class) for it to take effect

@Override
public void onUpgrade (sqliteDatabase db,int oldVersion,int newVersion) {
    db.executesql(DATABASE_CREATE2);
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>