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

标签 java android sqlite

我有用于缓存的数据库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 thisthis ,那没有解决我的问题。 提前致谢!

最佳答案

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/

相关文章:

java - 如何创建一个工厂,根据Java中的接口(interface)类型创建类?

java - 为什么 length() 用于在 Android 中的 json 解析中获取数组的大小?

python - python 中奇怪的静默崩溃与 : OSX, 请求、sqlite3、多处理的组合

android - 从服务器加载数据的最佳实践(Android App)

php - PDO SQLite 无法绑定(bind) NOT IN 表达式

java - 每次迭代多次使用 ArrayList.get() 时是否会对性能产生影响?

java - BND 在 'uses' 指令中包含同一包的导出包

java - 调试器不在堆栈帧中显示新变量

javascript - 如何通过@react-native-community/slider 在android中创建垂直 slider ?

Android SD 卡文件系统