抱歉打扰大家,但是有时候我要寻求帮助。我是Android开发的新手,目前我正在频道ProgrammingKnowledge上关注youtube上创建android数据库的教程,但是,它仅适用于一个表,而我需要在数据库中拥有3个表,并且无法获取我的方式。任何帮助,将不胜感激。提前致谢!
这是我目前拥有的代码。
公共类DatabaseHelper扩展了SQLiteOpenHelper {
//database name declaration public static final String DATABASE_NAME = "museum.db"; public static final String TABLE_NAME = "exponent_table"; public static final String COL_1 = "ID"; public static final String COL_2 = "TITLE"; public static final String COL_3 = "STORY"; public static final String COL_4 = "AUTHOR"; public static final String COL_5 = "DATE"; public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, 1); SQLiteDatabase db= this.getWritableDatabase(); //will create database and table, just for checking, will be replaced } @Override public void onCreate(SQLiteDatabase db) { //creating tables ??? db.execSQL("create table "+TABLE_NAME+" (ID INTEGER PRIMARY KEY AUTOINCREMENT, TITLE TEXT, STORY TEXT, AUTHOR TEXT, DATE STRING)"); } @Override public void onUpgrade(SQLiteDatabase db, int i, int i1) { db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME); onCreate(db); }
}
我在链接上看到了类似的内容: 多表数据库SQLite android
但是还是不明白。
这是一个很好的教程..阅读它
您可以像这样创建多个表
@Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table "+TABLE_NAME+" (ID INTEGER PRIMARY KEY AUTOINCREMENT, TITLE TEXT, STORY TEXT, AUTHOR TEXT, DATE STRING)"); db.execSQL("create table "+TABLE_NAME1+" (ID INTEGER PRIMARY KEY AUTOINCREMENT, Table1Field TEXT)"); } @Override public void onUpgrade(SQLiteDatabase db, int i, int i1) { db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME); db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME1); onCreate(db); }