简体   繁体   English

在sqlite android中获取现有行的插入语句

[英]Get Insert Statement for existing row in sqlite android

I have database A for caching and B at the server, so I want to send a row from A to B and for that I need to generate the insert statement for already existed row in A . 我在服务器上有数据库A用于缓存,数据库B ,因此我想从AB发送一行,为此,我需要为A中已经存在的行生成insert语句。

below is what I want to accomplish 以下是我要完成的工作

get insert select * from table where myId = 5;

and it should return 它应该返回

insert into table(myId,Col1,Col2,...) VALUES (5,'value1','value2',...);

I already had looked into this and this , that are not addressing my question. 我已经调查过这个这个 ,但都没有解决我的问题。 Thanks in advance! 提前致谢!

The sqlite3 command-line shell can generate such an output, but without column names, for queries: sqlite3命令行外壳程序可以为查询生成这样的输出,但没有列名:

sqlite> .mode insert MyTableName
sqlite> SELECT * FROM MyTable WHERE ID = 5;
INSERT INTO MyTableName VALUES(5,'value',NULL);

If you want the column names, you have to generate them manually: 如果需要列名,则必须手动生成它们:

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);

(The same string operations could be done in Java.) (可以在Java中完成相同的字符串操作。)

If your program does not know the exact database schema, you can read the list of columns for each table with PRAGMA table_info , and construct the statement from that: 如果您的程序不知道确切的数据库架构,则可以使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM