java – 如何在sqlite中的数据库中添加第二个表?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 如何在sqlite中的数据库中添加第二个表?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3867字,纯文字阅读大概需要6分钟。
内容图文
![java – 如何在sqlite中的数据库中添加第二个表?](/upload/InfoBanner/zyjiaocheng/901/9ccb959b13a24811ac76fd1596ad0b5e.jpg)
我有数据库考试指南,我已经创建了一个表table_subject和现在
我想在这个数据库中创建第二个表(table_chapter).我的问题是如何在现有数据库中添加此表?我有以下代码.任何帮助赞赏.
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);
}
此代码不会创建第二个表.我想在我的数据库中使用这两个表.
它在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)
解决方法:
创建另一个CREATE TABLE String,然后在onCreate中再次调用execSQL:
database.execSQL(DATABASE_CREATE1);
database.execSQL(DATABASE_CREATE2);
编辑
要将另一个表添加到现有数据库,请按如下所示修改onUpgrade方法.每当需要升级数据库时都会调用onUpgrade;请注意,您必须增加VERSION_NUMBER(您希望将其包含在类中的私有实例变量中)才能使其生效.
@Override
public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {
db.executeSQL(DATABASE_CREATE2);
}
内容总结
以上是互联网集市为您收集整理的java – 如何在sqlite中的数据库中添加第二个表?全部内容,希望文章能够帮你解决java – 如何在sqlite中的数据库中添加第二个表?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。