[英]Get Insert Statement for existing row in sqlite android
sqlite3
命令行外殼程序可以為查詢生成這樣的輸出,但沒有列名:
sqlite> .mode insert MyTableName
sqlite> SELECT * FROM MyTable WHERE ID = 5;
INSERT INTO MyTableName VALUES(5,'value',NULL);
如果需要列名,則必須手動生成它們:
SELECT 'INSERT INTO MyTable(a, b, c) VALUES (' ||
quote(a) || ',' ||
quote(b) || ',' ||
quote(c) || ');'
FROM MyTable
WHERE ID = 5;
--> INSERT INTO MyName(a, b, c) VALUES (42,'value',NULL);
(可以在Java中完成相同的字符串操作。)
如果您的程序不知道確切的數據庫架構,則可以使用PRAGMA table_info讀取每個表的列列表,並從中構造語句:
> create table MyTable(myId, Col1, Col2, [...]);
> pragma table_info(MyTable);
cid name type notnull dflt_value pk
---------- ---------- ---------- ---------- ---------- ----------
0 myId 0 0
1 Col1 0 0
2 Col2 0 0
3 ... 0 0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.