SQL 合并两个 SQLITE 数据库的最快方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9349659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fastest Way merge two SQLITE Databases
提问by subiet
I have 3 SQLite DBs, each having exactly the same set of 7 tables with respect to table structure. [They are Log Dumps from 3 different Machines].
我有 3 个 SQLite 数据库,每个数据库在表结构方面具有完全相同的 7 个表集。[它们是来自 3 台不同机器的日志转储]。
I want to combine them into one SQLite DB, having those very same 7 tables, but each table should have the combined data from all the three DBs. since I want to run queries across the 3 of them. What is the best, fastest way to do it.
我想将它们组合成一个 SQLite DB,具有相同的 7 个表,但每个表都应该包含来自所有三个 DB 的组合数据。因为我想对其中 3 个运行查询。什么是最好,最快的方法。
采纳答案by ccpizza
Export each database to an SQL dump and then import the dumps into your new combined database.
将每个数据库导出到 SQL 转储,然后将转储导入到新的组合数据库中。
For GUIs have a look at http://www.sqlite.org/cvstrac/wiki?p=ManagementTools
对于 GUI,请查看http://www.sqlite.org/cvstrac/wiki?p=ManagementTools
For example, with SQLiteStudiothat will be Database> Export the database: Export format: SQL> Done.
例如,对于SQLiteStudio将是Database> Export the database: Export format: SQL> Done。
回答by cheng chen
here is one way to merge two database with all tables of the same structure. I hope it could help.
这是将具有相同结构的所有表的两个数据库合并的一种方法。我希望它能有所帮助。
import sqlite3
con3 = sqlite3.connect("combine.db")
con3.execute("ATTACH 'results_a.db' as dba")
con3.execute("BEGIN")
for row in con3.execute("SELECT * FROM dba.sqlite_master WHERE type='table'"):
combine = "INSERT INTO "+ row[1] + " SELECT * FROM dba." + row[1]
print(combine)
con3.execute(combine)
con3.commit()
con3.execute("detach database dba")