我正在编写一个小实用程序来将数据从一个 sqlite 数据库文件复制到另一个。这两个文件具有相同的表结构 - 这完全是关于将行从一个数据库移动到另一个数据库。
我现在的代码:
let tables: Array<string> = [
"OneTable", "AnotherTable", "DataStoredHere", "Video"
]
tables.forEach((table) => {
console.log(`Copying ${table} table`);
sourceDB.each(`select * from ${table}`, (error, row) => {
console.log(row);
destDB.run(`insert into ${table} values (?)`, ...row) // this is the problem
})
})
row
这里是一个 js 对象,包含每个表中的所有键控数据。我确信有一种不涉及转义字符串化数据的简单方法。
最佳答案
如果你的数据库驱动没有阻塞ATTACH ,您可以简单地告诉数据库复制所有内容:
ATTACH '/some/where/source.db' AS src;
INSERT INTO main.MyTable SELECT * FROM src.MyTable;
关于node.js - 使用 node-sqlite 将数据从一个数据库复制到另一个数据库 - 格式化 'insert' 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43221905/