在sqlite中创build多个表的问题

我正在使用下面的代码在数据库中创build多个表。 但是,我不明白为什么会出现这个问题。

private static final String TABLE_SMSFilter = "SMSFilter"; public void onCreate(SQLiteDatabase db) { Log.d("Test", "Control is in Oncreate()"); String CREATE_SMSSSCHEDULE_TABLE = "CREATE TABLE " + TABLE_SMSSchedule + "(" + KEY_ID + " INTEGER PRIMARY KEY autoincrement," + KEY_NUMBER + " TEXT)"; String CREATE_PROFILE_SCHEDULE_TABLE = "CREATE TABLE " + TABLE_ProfileSchedule + "(" + ProfileSchedule_ID + " INTEGER PRIMARY KEY autoincrement," + ProfileSchedule_NUMBER + " TEXT, " + ProfileSchedule_ProfileMode + " TEXT," + ProfileSchedule_CalendarID + "INTEGER)"; String CREATE_SMS_FILTER_TABLE = "CREATE TABLE " + TABLE_SMSFilter + "(" + SMSFilter_ID + " INTEGER PRIMARY KEY autoincrement," + SMSFilter_NUMBER + " TEXT)"; db.execSQL(CREATE_SMSSSCHEDULE_TABLE); db.execSQL(CREATE_PROFILE_SCHEDULE_TABLE); db.execSQL(CREATE_SMS_FILTER_TABLE); } // Upgrading database @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // Drop older table if existed db.execSQL("DROP TABLE IF EXISTS " + TABLE_SMSSchedule); db.execSQL("DROP TABLE IF EXISTS " + TABLE_ProfileSchedule); db.execSQL("DROP TABLE IF EXISTS " + TABLE_SMSFilter); // Create tables again onCreate(db); } 

这里是插入function

 public void addSMSFilter(SMSFilter filterVariable) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(SMSFilter_NUMBER, filterVariable.getPhoneNumber()); // Contact // Name // values.put(KEY_PH_NO, contact.getPhoneNumber()); // Contact Phone Log.d("test", "inserting" + filterVariable.getPhoneNumber()); // Inserting Row db.insert(TABLE_SMSFilter, null, values); db.close(); // Closing database connection } 

现在在我的主要活动中,我用这个代码插入到这个表中

 DatabaseHandler db = new DatabaseHandler(this); SMSFilter sms = new SMSFilter("1234556"); db.addSMSFilter(sms); 

但它给了我没有find“SMSFILTER”表的错误。

日志结果在这里

 11-25 22:52:22.643: I/Database(12209): sqlite returned: error code = 1, msg = no such table: SMSFilter 11-25 22:52:22.643: E/Database(12209): Error inserting PhoneNo=1234556 11-25 22:52:22.643: E/Database(12209): android.database.sqlite.SQLiteException: no such table: SMSFilter: , while compiling: INSERT INTO SMSFilter(PhoneNo) VALUES(?); 11-25 22:52:22.643: E/Database(12209): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method) 11-25 22:52:22.643: E/Database(12209): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91) 11-25 22:52:22.643: E/Database(12209): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64) 11-25 22:52:22.643: E/Database(12209): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80) 11-25 22:52:22.643: E/Database(12209): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:36) 11-25 22:52:22.643: E/Database(12209): at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1212) 11-25 22:52:22.643: E/Database(12209): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1610) 11-25 22:52:22.643: E/Database(12209): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1484) 11-25 22:52:22.643: E/Database(12209): at com.scheduler.database.DatabaseHandler.addSMSFilter(DatabaseHandler.java:327) 11-25 22:52:22.643: E/Database(12209): at com.schedule.test.TestActivity.onCreate(TestActivity.java:31) 11-25 22:52:22.643: E/Database(12209): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069) 11-25 22:52:22.643: E/Database(12209): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751) 11-25 22:52:22.643: E/Database(12209): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803) 11-25 22:52:22.643: E/Database(12209): at android.app.ActivityThread.access$2300(ActivityThread.java:135) 11-25 22:52:22.643: E/Database(12209): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136) 11-25 22:52:22.643: E/Database(12209): at android.os.Handler.dispatchMessage(Handler.java:99) 11-25 22:52:22.643: E/Database(12209): at android.os.Looper.loop(Looper.java:144) 11-25 22:52:22.643: E/Database(12209): at android.app.ActivityThread.main(ActivityThread.java:4937) 11-25 22:52:22.643: E/Database(12209): at java.lang.reflect.Method.invokeNative(Native Method) 11-25 22:52:22.643: E/Database(12209): at java.lang.reflect.Method.invoke(Method.java:521) 11-25 22:52:22.643: E/Database(12209): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 11-25 22:52:22.643: E/Database(12209): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 11-25 22:52:22.643: E/Database(12209): at dalvik.system.NativeStart.main(Native Method) 

Solutions Collecting From Web of "在sqlite中创build多个表的问题"

在更改databse version number时调用onUpgrade 。 在更新表格时,您需要增加数据库version number ,在onCreate方法中指定新的表格创build查询,并将ALTER TABLE置于onUpgrade方法以更新以前版本的表格。 当Android检测到数据库版本不匹配时,它会自动调用onUpgrade方法

请注意, onCreate仅在第一次创build数据库物理文件时调用。 看来, onCreate方法没有被调用,因为数据库已经存在于SDCard的某个地方。 因此表SMSFilter不创build。 您需要将onCreate中的代码复制到onUpgrade方法(在drop table命令之后)。 此外,要触发onUpgrade,您必须增加数据库版本。

 String CREATE_PROFILE_SCHEDULE_TABLE = "CREATE TABLE " + TABLE_ProfileSchedule + "(" + ProfileSchedule_ID + " INTEGER PRIMARY KEY autoincrement," + ProfileSchedule_NUMBER + " TEXT, " + ProfileSchedule_ProfileMode + " TEXT," + ProfileSchedule_CalendarID + "INTEGER)"; 

更新这个

 String CREATE_PROFILE_SCHEDULE_TABLE = "CREATE TABLE " + TABLE_ProfileSchedule + "(" + ProfileSchedule_ID + " INTEGER PRIMARY KEY autoincrement," + ProfileSchedule_NUMBER + " TEXT, " + ProfileSchedule_ProfileMode + " TEXT," + ProfileSchedule_CalendarID + " INTEGER)"; 

您的日程表创build代码不工作,因为在最后一行没有空间B / W整数和列名称正在创build问题。