我有用于缓存的数据库A和服务器上的B,所以我想从A发送一行到B 为此,我需要为 A 中已存在的行生成插入语句。
下面是我想要完成的
get insert select * from table where myId = 5;
并且它应该返回
insert into table(myId,Col1,Col2,...) VALUES (5,'value1','value2',...);
I already had looked into this和 this ,那没有解决我的问题。 提前致谢!
最佳答案
sqlite3
命令行 shell 可以生成这样的输出,但没有列名,用于查询:
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
关于java - 在sqlite android中获取现有行的插入语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35153883/