【问题标题】:Get Insert Statement for existing row in sqlite android获取sqlite android中现有行的插入语句
【发布时间】:2016-05-11 06:46:51
【问题描述】:

我在服务器上有用于缓存的数据库 AB,所以我想将一行从 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 thisthis,这并没有解决我的问题。 提前致谢!

【问题讨论】:

  • @duffymo 您指的是在同一个物理数据库上将行从 table1 复制到 table2,而我的情况并非如此。

标签: java android sqlite


【解决方案1】:

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         

【讨论】:

  • 感谢您的回复。但我无法指定列,因为它们在表格中的数量非常大。似乎我无法实现我提出的问题。
猜你喜欢
  • 2011-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-10
  • 2012-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多