我需要在我的Android应用中将数据从一个表移动到另一个表
我想使用以下sql:
insert into MYTABLE2 select id, STATUS risposta, DATETIME('now') data_ins from MYTABLE 2
不幸的是,在表MYTABLE2中有一个_ID列AUTOINCREMENT。 我该怎么办?
感谢。
修改 这是我的MYTABLE2,我希望用另一个表中的数据填充表格:
CREATE TABLE "ANSWERS" ("_id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL ,
"ID_QUESTION" INTEGER,"DATE_INS" DATETIME DEFAULT
(CURRENT_DATE) , "ANSWER" INTEGER)
答案 0 :(得分:52)
在INSERT
子句
INSERT INTO destinationTable (risposta, data_ins)
SELECT STATUS risposta, DATETIME('now') data_ins
FROM sourceTable
答案 1 :(得分:3)
您可以指定要插入的列。假设_id
列为autoincrement
并且您要插入其他两列,则可以使用以下内容:
insert into MYTABLE2 (riposta, data_ins)
select STATUS risposta, DATETIME('now') data_ins from MYTABLE 2
答案 2 :(得分:2)
这可能有所帮助,从一个表到另一个表的查询,它还将检查所选列(id)是否已存在于另一个表中。
SQLite QUERY:
INSERT INTO MYTABLE2(id,data_ins )
SELECT id, data_ins FROM MYTABLE2
WHERE id NOT IN ( SELECT id FROM MYTABLE1)
机器人:
String select_insert_query = "INSERT INTO " + TABLE_MYTABLE2
+ "( " + ID + "," + DATA_INS + ") SELECT "
+ ID + ","
+ DATA_INS + " FROM "
+ TABLE_MYTABLE2
+ " WHERE " + ID + " NOT IN (SELECT " + ID
+ " FROM " + TABLE_MYTABLE1 + ")";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(select_insert_query, null);
cursor.close();