如何添加第二个表在数据库SQLite中? [英] How to add second table in database in sqlite?
问题描述
我有数据库 examguide
,我已经创建了一个表 table_subject
现在
我想在这个数据库中创建第二个表( table_chapter
)。我的问题是如何在现有的数据库中添加这个表?我有以下code。任何帮助AP preciated。
私有静态最后弦乐DATABASE_CREATE =CREATE TABLE IF NOT EXISTS
+ TABLE_SUBJECT +(+ COLUMN_ID
+整数主键自动增量,
+ COLUMN_SUBJECT +文字不为空,
+ COLUMN_CHAPTER +文本
+ COLUMN_QUESTION +文字不为空,
+ COLUMN_OPTIONA +文字不为空,
+ COLUMN_OPTIONB +文字不为空,
+ COLUMN_OPTIONC +文字不为空,
+ COLUMN_OPTIOND +文字不为空,
+ COLUMN_CORRECT +文字不为空,
+ COLUMN_CONFIRM +文字NOT NULL);;
私有静态最后弦乐DATABASE_CREATE1 =CREATE TABLE IF NOT EXISTS
+ TABLE_CHAPTER +(+ COLUMN_ID
+整数主键自动增量,
+ COLUMN_SUBJECT +文字不为空,
+ COLUMN_CHAPTER +文本
+ COLUMN_QUESTION +文字不为空,
+ COLUMN_OPTIONA +文字不为空,
+ COLUMN_OPTIONB +文字不为空,
+ COLUMN_OPTIONC +文字不为空,
+ COLUMN_OPTIOND +文字不为空,
+ COLUMN_CORRECT +文字不为空,
+ COLUMN_CONFIRM +文字NOT NULL);;
公共MySQLiteHelper的open()抛出的SQLException
{
DB = this.getWritableDatabase();
回到这一点;
}
公共MySQLiteHelper(上下文的背景下)
{
超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
}
@覆盖
公共无效的onCreate(SQLiteDatabase数据库){
database.execSQL(DATABASE_CREATE);
database.execSQL(DATABASE_CREATE1);
}
这code不能创建第二个表。我想这两个表在我的数据库。
这表明继logcat的错误。
03-21 18:31:06.551:ERROR /数据库(8255):错误插入章节=分页correctoption =卷影副本craete的一页标题=操作系统问题复印件=什么是卷影副本? optiona =卷影副本craete页面确认的副本= YES optionb = sahdow复制创建页面OptionC中=卷影副本删除重复的页面optiond =卷影副本删除原始创造卷影副本
03-21 18:31:06.551:ERROR /数据库(8255):android.database.sqlite.SQLiteException:没有这样的表:第一章:在编制:INSERT INTO章(章correctoption,主题,问题,optiona,确认, optionb,OptionC中,optiond)VALUES(?,,,,,,,)????????;
03-21 18:31:06.551:ERROR /数据库(8255):在android.database.sqlite.SQLiteCompiledSql.native_compile(本机方法)
03-21 18:31:06.551:ERROR /数据库(8255):在android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
03-21 18:31:06.551:ERROR /数据库(8255):在android.database.sqlite.SQLiteCompiledSql< INIT>(SQLiteCompiledSql.java:65)。
03-21 18:31:06.551:ERROR /数据库(8255):在android.database.sqlite.SQLiteProgram< INIT>(SQLiteProgram.java:83)。
03-21 18:31:06.551:ERROR /数据库(8255):在android.database.sqlite.SQLiteStatement< INIT>(SQLiteStatement.java:41)。
03-21 18:31:06.551:ERROR /数据库(8255):在android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1149)
03-21 18:31:06.551:ERROR /数据库(8255):在android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1569)
03-21 18:31:06.551:ERROR /数据库(8255):在android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1426)
03-21 18:31:06.551:ERROR /数据库(8255):在com.example.examguide.MySQLiteHelper.insertChapterData(MySQLiteHelper.java:212)
03-21 18:31:06.551:ERROR /数据库(8255):在com.example.examguide.ObjectiveAddActivity $ 2.onClick(ObjectiveAddActivity.java:155)
请另一个 CREATE TABLE
字符串,然后在你的的onCreate
,呼叫 execSQL
一次:
database.execSQL(DATABASE_CREATE1);
database.execSQL(DATABASE_CREATE2);
修改
要添加其他表已经存在的数据库,修改 onUpgrade
方法如下。 onUpgrade
时,要调用数据库需要进行升级;请注意,您必须递增 VERSION_NUMBER
(你将要包括在类私有实例变量),以便使其生效。
@覆盖
公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
db.executeSQL(DATABASE_CREATE2);
}
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)
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);
}
这篇关于如何添加第二个表在数据库SQLite中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!