我有3个SQLite数据库,每个数据库都有完全相同的7个表的表结构。它们是来自3台不同机器的日志转储。
我想将它们合并到一个SQLite DB中,具有非常相同的7个表,但每个表都应该包含来自所有三个DB的组合数据。因为我想在其中的3个上运行查询。最好、最快的方法是什么?
发布于 2012-02-19 21:51:52
将每个数据库导出到SQL转储,然后将转储导入到新的组合数据库中。
对于GUI,可以查看http://www.sqlite.org/cvstrac/wiki?p=ManagementTools
例如,对于SQLiteStudio,将是数据库>导出数据库:Export format:SQL > Done。
使用命令行sqlite实用程序(在linux repos中可用,并且通常已经存在ootb),您可以通过以下步骤创建转储:
# starts the interactive prompt
sqlite3 my_database.sqlite
sqlite> .output my_dump.sql
sqlite> .exit要从交互式提示符导入转储文件,请执行以下操作:
sqlite> .read export.sqlite3.sql您也可以直接从shell导入:
cat my_dump.sql | sqlite3 my_database.sqlite发布于 2016-05-10 20:25:07
这是一种合并两个具有相同结构的表的数据库的方法。我希望它能有所帮助。
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")https://stackoverflow.com/questions/9349659
复制相似问题