San*_*til 3 java sqlite android
我有数据库examguide,我已经创建了一个表table_subject,现在
我想table_chapter在这个数据库中创建第二个table().我的问题是如何在现有数据库中添加此表?我有以下代码.任何帮助赞赏.
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);
}
Run Code Online (Sandbox Code Playgroud)
此代码不会创建第二个表.我想在我的数据库中使用这两个表.
它在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)
Run Code Online (Sandbox Code Playgroud)
制作另一个CREATE TABLE字符串,然后在你的onCreate,execSQL再次打电话:
database.execSQL(DATABASE_CREATE1);
database.execSQL(DATABASE_CREATE2);
Run Code Online (Sandbox Code Playgroud)
编辑
要将另一个表添加到现有数据库,请onUpgrade按如下方式修改方法.onUpgrade每当数据库需要升级时调用; 请注意,您必须增加VERSION_NUMBER(您希望在类中包含为私有实例变量)才能使其生效.
@Override
public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {
db.executeSQL(DATABASE_CREATE2);
}
Run Code Online (Sandbox Code Playgroud)
如果要将表添加到现有数据库,请使用以下onUpgrade() 方法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);
}
Run Code Online (Sandbox Code Playgroud)
并且还会增加传递给MySqliteHelper构造函数的数据库版本的值(如果传递1然后传递2).
| 归档时间: |
|
| 查看次数: |
7671 次 |
| 最近记录: |