-1
我有我已經創建了一個表table_comments數據庫comments.db現在 我想在這個數據庫中創建第二個表(table_birthday)。我的問題是如何在現有數據庫中添加此表?我有以下代碼。任何幫助讚賞。如何添加第二個表中的SQLite數據庫
package com.example.proj1;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class MySQLiteHelper extends SQLiteOpenHelper {
//table 1
public static final String TABLE_COMMENTS = "comments";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_COMMENT = "comment";
//table 2
public static final String TABLE_BIRTHDAY = "birthday";
public static final String Birthday_ID = "_id";
public static final String COLUMN_Birthday = "bday";
//database
private static final String DATABASE_NAME = "commments.db";
private static final int DATABASE_VERSION = 1;
// table 1
private static final String DATABASE_CREATE = "create table "
+ TABLE_COMMENTS + "(" + COLUMN_ID
+ " integer primary key autoincrement, " + COLUMN_COMMENT
+ " text not null);";
//table 2
private static final String DATABASE_CREAT = "create table "
+ TABLE_BIRTHDAY + "(" + Birthday_ID
+ " integer primary key autoincrement, " + COLUMN_Birthday
+ " text not null);";
public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
database.execSQL(DATABASE_CREAT);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(MySQLiteHelper.class.getName(),"Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_COMMENTS);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_BIRTHDAY);
onCreate(db);
}
}
這段代碼應該這樣做......問題是什麼? – Kristopher
http://stackoverflow.com/questions/9804083/how-to-add-second-table-in-database-in-sqlite 這個問題已經解決只是看到鏈接 – Arpan24x7