我需要创建一个包含 3 个表的数据库,我这样做如下:
public class DatabaseUtils extends SQLiteOpenHelper {
private final Context myContext;
private SQLiteDatabase DataBase;
// Database creation sql statement
private static final String CREATE_TABLE_CS = "create table "+ TABLE_CS + "(" + COLUMN_CS + " TEXT NOT NULL, " + COLUMN_CE_CID + " INTEGER NOT NULL, "+ COLUMN_CE_PID +" INTEGER NOT NULL);";
private static final String CREATE_TABLE_SS = "create table "+ TABLE_SS + "(" + COLUMN_SS + " TEXT NOT NULL, " + COLUMN_SUB_CID + " INTEGER NOT NULL, "+ COLUMN_SUB_PID +" INTEGER NOT NULL);";
private static final String CREATE_TABLE_AS = "create table "+ TABLE_AS + "(" + COLUMN_AS + " TEXT NOT NULL, " + COLUMN_CID + " INTEGER NOT NULL, "+ COLUMN_AID +" INTEGER NOT NULL);";
public DatabaseUtils(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
DATABASE_PATH = Environment.getDataDirectory().getAbsolutePath() + "/" +"data/"+ context.getResources().getString(R.string.app_package);
this.myContext = context;
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(CREATE_TABLE_CS);
database.execSQL(CREATE_TABLE_SS);
database.execSQL(CREATE_TABLE_AS);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(DatabaseUtils.class.getName(),"Upgrading database from version " + oldVersion + " to "+ newVersion + ", which will destroy all old data");
//db.execSQL("DROP TABLE IF EXISTS " + TABLE_COMMENTS);
onCreate(db);
}
}
在我的活动中,我正在如下调用DatabaseUtils
类onCreate
:
DatabaseUtils db = new DatabaseUtils(this);
但数据库不是用 3 个表创建的。我究竟做错了什么?顺便说一句,我所有的字符串值都正确。请帮助我如何创建数据库。