我在这里遇到了一个非常有趣的漏洞。我似乎无法为现有的数据库创建第二个数据库表。我基本上希望它们具有相同的结构和相同的列,换句话说,是我现有表的副本。我的目标是将值从第一个表复制到第二个表。我创建的第一张表是我已经使用过的。
我已经尝试了我所知道的一切,未经安装并重新安装应用程序以清除数据库,更改了表的名称,版本..但是我一直得到“表未找到异常”。请问任何想法可能导致这种情况以及如何解决这个问题?谢谢
这里是两个人的创作:
/* tables variable*/
public static final String DATABASE_TABLE = "Memo";
public static final String ROUTE = "Route";
public static final String KEY_ID = "_id";
public static final String NAME = "Title";
public static final String DATE = "date";
public static final String TIME = "time";
public static final String VENUE = "venue";
public static final String CATEGORY = "category";
public static final String PRIORITY = "priority";
public static final String NOTES = "notes";
private static final String DATABASE_CREATE = "create table Memo (_id integer primary key autoincrement,"
+ "Title text, date text, time text,"
+ "venue text, category text,"
+ "priority text, notes text);";
private static final String DATABASE_CREATE_ROUTE = "create table Route (_id integer primary key autoincrement,"
+ "Title text, date text, time text,"
+ "venue text, category text,"
+ "priority text, notes text);";
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
Log.d(TAG, "created Memo table");
db.execSQL(DATABASE_CREATE_ROUTE );
Log.d(TAG, "created Route table");
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG,"upgrading Database from version"+ oldVersion+ "to"
+newVersion+ "which old data will be lost");
db.execSQL("DROP TABLE IF EXITS Memo");
db.execSQL("DROP TABLE IF EXITS Route");
onCreate(db);
}
}
public void copyData(long rowId){
db.execSQL("insert into ROUTE select * from DATABASE_TABLE WHERE KEY_ID = rowId");
}
Logcat异常是:
10-22 15:47:17.326: ERROR/Database(807): Failure 1 (no such table: ROUTE) on 0x1d7f20 when preparing 'insert into ROUTE select * from DATABASE_TABLE WHERE KEY_ID = rowId'.
10-22 15:47:17.356: ERROR/AndroidRuntime(807): Uncaught handler: thread main exiting due to uncaught exception
10-22 15:47:17.526: ERROR/AndroidRuntime(807): android.database.sqlite.SQLiteException: no such table: ROUTE: insert into ROUTE select * from DATABASE_TABLE WHERE KEY_ID = rowId
10-22 15:47:17.526: ERROR/AndroidRuntime(807): at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method)
10-22 15:47:17.526: ERROR/AndroidRuntime(807): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1496)
10-22 15:47:17.526: ERROR/AndroidRuntime(807): at com.MemoManager.DBAdapter.copyData(DBAdapter.java:265)
答案 0 :(得分:2)
它是否可以引用插入字符串中的DATABASE_TABLE?我认为它需要 “插入ROUTE select * from”+ DATABASE_TABLE +“WHERE KEY_ID = rowId”
编辑:我认为Aaron可能也会纠正/而不是。
答案 1 :(得分:1)
我认为您在DATABASE CREATE和DATABASE CREATE ROUTE中缺少父级
private static final String DATABASE_CREATE = "create table Memo (_id integer primary key autoincrement,"
+ "Title text, date text, time text,"
+ "venue text, category text,"
+ "priority text, notes text;";
^ (HERE?)