我要将数据移动到一个表到另一个表。 新表将作为旧表但没有约束(在冲突替换时),因此列是相同的。
这是我要做的查询:INSERT INTO TABLE2 SELECT * FROM TABLE1
我遇到了一个大问题,因为我认为我不能用以下方式执行这种查询:
db.execSQL("INSERT INTO " + Tables.TRACKS + "_2 SELECT * FROM " + Tables.TRACKS );
execSQL因此错误而失败(db onUpgrade):
06-04 12:25:50.000: D/SqliteDatabaseCpp(17817): Registering sqlite logging func: /data/data/com.musixmatch.android.lyrify/databases/library.db
06-04 12:25:50.000: D/SqliteDatabaseCpp(17817): DB info: open db, path = /data/data/com.musixmatch.android.lyrify/databases , key = lafvYvq, flag = 6, file size = 101376
06-04 12:25:50.000: D/SqliteDatabaseCpp(17817): DB info: path = /data/data/com.musixmatch.android.lyrify/databases , key = lafvYvq, handle: 0x1bd8bd8, type: w, r/w: (0,1), mode: delete, disk free size: 1243 M
06-04 12:25:50.010: D/SqliteDatabaseCpp(17817): DB info: close db, path = /data/data/com.musixmatch.android.lyrify/databases , key = lafvYvq, handle = 0x1bd8bd8, type = w, r/w = (0, 0)
06-04 12:25:50.030: E/SQLiteOpenHelper(17817): Couldn't open library.db for writing (will try read-only):
06-04 12:25:50.030: E/SQLiteOpenHelper(17817): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
06-04 12:25:50.030: E/SQLiteOpenHelper(17817): at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
06-04 12:25:50.030: E/SQLiteOpenHelper(17817): at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:95)
06-04 12:25:50.030: E/SQLiteOpenHelper(17817): at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:2008)
06-04 12:25:50.030: E/SQLiteOpenHelper(17817): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1948)
06-04 12:25:50.030: E/SQLiteOpenHelper(17817): at com.designfuture.music.provider.MusicDatabase.alterTrackFragmentForFileIDSupport(MusicDatabase.java:316)
06-04 12:25:50.030: E/SQLiteOpenHelper(17817): at com.designfuture.music.provider.MusicDatabase.onUpgrade(MusicDatabase.java:242)
06-04 12:25:50.030: E/SQLiteOpenHelper(17817): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:188)
06-04 12:25:50.030: E/SQLiteOpenHelper(17817): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:249)
06-04 12:25:50.030: E/SQLiteOpenHelper(17817): at com.designfuture.music.provider.MusicProvider.query(MusicProvider.java:199)
06-04 12:25:50.030: E/SQLiteOpenHelper(17817): at android.content.ContentProvider$Transport.query(ContentProvider.java:189)
06-04 12:25:50.030: E/SQLiteOpenHelper(17817): at android.content.ContentResolver.query(ContentResolver.java:315)
06-04 12:25:50.030: E/SQLiteOpenHelper(17817): at android.content.AsyncQueryHandler$WorkerHandler.handleMessage(AsyncQueryHandler.java:94)
06-04 12:25:50.030: E/SQLiteOpenHelper(17817): at android.os.Handler.dispatchMessage(Handler.java:99)
06-04 12:25:50.030: E/SQLiteOpenHelper(17817): at android.os.Looper.loop(Looper.java:156)
06-04 12:25:50.030: E/SQLiteOpenHelper(17817): at android.os.HandlerThread.run(HandlerThread.java:60)
06-04 12:25:50.030: D/SqliteDatabaseCpp(17817): DB info: open db, path = /data/data/com.musixmatch.android.lyrify/databases , key = lafvYvq, flag = 1, file size = 101376
06-04 12:25:50.030: D/SqliteDatabaseCpp(17817): DB info: path = /data/data/com.musixmatch.android.lyrify/databases , key = lafvYvq, handle: 0x1bec760, type: r, r/w: (1,0), mode: delete, disk free size: 1243 M
06-04 12:25:50.030: D/SqliteDatabaseCpp(17817): DB info: close db, path = /data/data/com.musixmatch.android.lyrify/databases , key = lafvYvq, handle = 0x1bec760, type = r, r/w = (0, 0)
06-04 12:25:50.040: W/AsyncQuery(17817): Exception thrown during handling EVENT_ARG_QUERY
06-04 12:25:50.040: W/AsyncQuery(17817): android.database.sqlite.SQLiteException: Can't upgrade read-only database from version 7 to 9: /data/data/com.musixmatch.android.lyrify/databases/library.db
06-04 12:25:50.040: W/AsyncQuery(17817): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:262)
06-04 12:25:50.040: W/AsyncQuery(17817): at com.designfuture.music.provider.MusicProvider.query(MusicProvider.java:199)
06-04 12:25:50.040: W/AsyncQuery(17817): at android.content.ContentProvider$Transport.query(ContentProvider.java:189)
06-04 12:25:50.040: W/AsyncQuery(17817): at android.content.ContentResolver.query(ContentResolver.java:315)
06-04 12:25:50.040: W/AsyncQuery(17817): at android.content.AsyncQueryHandler$WorkerHandler.handleMessage(AsyncQueryHandler.java:94)
06-04 12:25:50.040: W/AsyncQuery(17817): at android.os.Handler.dispatchMessage(Handler.java:99)
06-04 12:25:50.040: W/AsyncQuery(17817): at android.os.Looper.loop(Looper.java:156)
06-04 12:25:50.040: W/AsyncQuery(17817): at android.os.HandlerThread.run(HandlerThread.java:60)
我认为这是因为我在查询中有一个SELECT ..这是来自Android文档:http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#execSQL%28java.lang.String%29
执行一个非SELECT或任何其他SQL的SQL语句 返回数据的语句。
如果我使用
db.rawQuery( "INSERT INTO " + Tables.TRACKS + "_2 SELECT * FROM " + Tables.TRACKS, null );
它执行没有错误,但它没有做任何事情。我认为这是因为rawQuery用于执行SELECT查询而不会写入数据库。
UPDATE :如果我选择所有带有正常查询的记录,那么使用循环我将它们插入到新表中一切正常但这不是优化的方式,我不希望有缓慢升级的应用
UPDATE2 :
这两个表格之间的唯一区别是:
新表有3个新的字符串列,它们可以为null 新表没有旧表具有的唯一(ON CONFLICT REPLACE)约束
这两个表都有此字段:BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT"
。我是否要从插入中排除该字段?还要记住第二个平板电脑在升级时是空的(我在升级时创建它)
那么我该怎样做才能执行需要与我同时编写和读取的查询?
答案 0 :(得分:4)
由于您没有发布表格布局,让我们从以下内容开始:
CREATE TABLE TABLE1 (
_id INTEGER PRIMARY KEY,
value1 TEXT,
value2 TEXT
);
CREATE TABLE TABLE2 (
_id INTEGER PRIMARY KEY,
value2 TEXT,
value1 TEXT,
value3 TEXT,
value4 TEXT,
value5 TEXT
);
要将TABLE1
中的值正确插入TABLE2
,您必须考虑该顺序&列数重要&必须匹配(&如果你有主键,从插入和选择语句中省略它们总是一个好主意 - 不要去复制那样的东西。)
INSERT INTO TABLE2 (value1, value2) SELECT value1, value2 FROM TABLE1