SQLite数据库android创建表 [英] SQLite database android create table
问题描述
我收到错误没有这样的表"并导致我尽一切可能让它消失,现在它消失了,但我很困惑.这是我的情况
"I got error "No Such Table" and cause me to try everything possible to make it gone, now it's gone, but i'm confuse. Here is my situation
我需要在应用程序中创建 2 个表.我在每张桌子上放了一个班级,它看起来像下面的代码.如果我按照此操作,当我从Table2"中提取数据时,它会显示No such Table",因此我需要将 DATABASE_NAME 更改为其他值 =DB2";那么Table2"将成功创建并能够提取数据.
I have 2 tables need to create in the apps. and i put one class per table and it's look like code below. If i follow this it will hit me with "No Such Table" when i pull data from "Table2", so i need to change the DATABASE_NAME to some other value = "DB2"; then the "Table2" will create successfully and able to pull out data.
所以我的问题是这是确保成功创建 2 个表的正确方法吗?或者我不应该将表分成 2 个类?请指教
So my question is this a correct way to make sure 2 tables create successfully? or i should not seperate table into 2 classes? Pls advise
public class DB1 extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "DB";
// Labels table name
private static final String TABLE_STORE = "Table1";
// Labels Table Columns names
public static final String KEY_1 = "col1";
public RC_StoreTbl(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
// Category table create query
String CREATE_TABLE = "CREATE TABLE " + TABLE_STORE + "("
+KEY_1 + " INTEGER PRIMARY KEY)";
db.execSQL(CREATE_TABLE);
}
}
另一堂课
public class DB2 extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "DB";
// Labels table name
private static final String TABLE_STORE = "Table2";
// Labels Table Columns names
public static final String KEY_1 = "col1";
public RC_StoreTbl(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
// Category table create query
String CREATE_TABLE = "CREATE TABLE " + TABLE_STORE + "("
+KEY_1 + " INTEGER PRIMARY KEY)";
db.execSQL(CREATE_TABLE);
}
}
推荐答案
不,您应该将表放在一个数据库类中.只要确保每当您更改代码以修改表或添加/删除表时,您都会清除数据或卸载应用程序并再次运行.您很可能会收到没有这样的表"错误,因为您添加或修改了一个表,在这种情况下,onCreate 不会被再次调用,直到您清除数据或卸载您的应用程序.
No you should put tables in one database class. Just make sure that whenever you change your code to modify a table or add/remove a table that you clear your data or uninstall your app and run again. You most likely get "No Such Table" error because you add or modify a table, in this case onCreate would not be called again until you clear data or uninstall your app.
这篇关于SQLite数据库android创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!