I have database examguide
and I already created one table table_subject
and now
I want to create second table (table_chapter
) in this database. My question is how do I add this table in the existing database? I have following code. Any help appreciated.
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 not create second table. I want both table in my database.
it show 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)
If you want to add the table to the already existing database then use the onUpgrade()
method of the MySqliteHelper
:
@Override
public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {
// Create the string for the second table creation
db.executeSQL(DATABASE_CREATE_SECOND_TABLE);
}
and also increment the value of the database version that you pass to the MySqliteHelper
constructor(if you pass 1 then pass 2).
Make another CREATE TABLE
String and then in your onCreate
, call execSQL
once more:
database.execSQL(DATABASE_CREATE1);
database.execSQL(DATABASE_CREATE2);
Edit
To add another table to an already existing database, modify your onUpgrade
method as follows. onUpgrade
is called whenever the database needs to be upgraded; note that you must increment the VERSION_NUMBER
(which you will want to include as a private instance variable in your class) in order for this to take effect.
@Override
public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {
db.executeSQL(DATABASE_CREATE2);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With