如何在sqlite中添加数据库中的第二个表?

时间:2012-03-21 11:50:59

标签: java android sqlite

我有数据库examguide,我已经创建了一个表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)

5 个答案:

答案 0 :(得分:9)

制作另一个CREATE TABLE字符串,然后在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);
}

答案 1 :(得分:6)

如果要将表添加到现有数据库,请使用onUpgrade()的{​​{1}}方法:

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); } 构造函数的数据库版本的值(如果传递1然后传递2)。

答案 2 :(得分:3)

使用此代码可以创建多个表

    private static final String ALERT_DATABASE="alerts.db";

//Database Version of Alert System  
private static final int ALERT_DATABASE_VERSION=1;

//Create alert_type table 
private static final String CREATE_ALERT_TYPE="CREATE TABLE "
        +ALERT_TYPE+"( "+ALERT_TYPE_ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"
        +ALERT_TYPE_NAME+" TEXT,"+ALERT_TYPE_TONE+" VARCHAR)";  


private static final String CREATE_ALERT_INFORMATION="CREATE TABLE "
        +ALERT_INFO+"( "+ALERT_INFO_ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"
        +ALERT_INFO_TITLE+" TEXT,"
        +ALERT_INFO_DATE+" VARCHAR,"
        +ALERT_INFO_TIME+" VARCHAR,"
        +ALERT_INFO_DESCRIPTION+" VARCHAR,"
        +ALERT_INFO_TYPE_ID+" VARCHAR)";


public AlertDatabase(Context context) {
    super(context, ALERT_DATABASE, null, ALERT_DATABASE_VERSION);
    // TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase alertdatabase){
    alertdatabase.execSQL(CREATE_ALERT_TYPE);
    alertdatabase.execSQL(CREATE_ALERT_INFORMATION);
}

答案 3 :(得分:3)

如果要将表添加到现有数据库,则必须执行以下操作: 您正在使用帮助程序类。这意味着在超导体中您正在解析版本号。在第一次创建这个帮助器对象时,它将调用创建初始表的onCreate方法。然后,只要您不更改版本号,当您使用创建类的新实例时,将不会调用任何函数。但是帮助器还包含onUpgrade方法。只有在超级构造函数中解析的版本号高于之前使用的版本号时,才会调用此函数。那你需要做什么:

  1. 增加常数DATABASE-VERSION
  2. 覆盖onUpgrade功能并在DB.execsql(yourtablecreationstring)
  3. 内添加

答案 4 :(得分:1)

您将无法使用onCreate()方法创建第二个表,因为只有在创建数据库时才会调用它。您可以使用onUpgrade()或创建新方法来执行此操作。在升级时使用有一些限制,因为在更改version时会调用它。

所以最好的方法是在Helper Class中添加一个新方法。

方法可能看起来像这样,

    public void AddnewTable(){
    //At first you will need a Database object.Lets create it.
    SQLiteDatabase ourDatabase=this.getWritableDatabase();

    ourDatabase.execSQL(CreateTableString)//CreateTableString is the SQL Command String        
    }