我有两个不同的 SQLite 数据库 XXX 和 YYY。
XXX包含表A,YYY包含表B。
A 和 B 具有相同的结构(列)。
如何在 Python - SQLite API 中将 B 的行追加到 A 中。
追加 A 后包含 A 行和 B 行。
您首先使用以下命令获得与数据库的连接sqlite3.connect
,然后创建一个游标以便您可以执行sql。一旦有了游标,就可以执行任意的sql命令。
Example:
import sqlite3
# Get connections to the databases
db_a = sqlite3.connect('database_a.db')
db_b = sqlite3.connect('database_b.db')
# Get the contents of a table
b_cursor = db_b.cursor()
b_cursor.execute('SELECT * FROM mytable')
output = b_cursor.fetchall() # Returns the results as a list.
# Insert those contents into another table.
a_cursor = db_a.cursor()
for row in output:
a_cursor.execute('INSERT INTO myothertable VALUES (?, ?, ...etc..., ?, ?)', row)
# Cleanup
db_a.commit()
a_cursor.close()
b_cursor.close()
警告:我还没有实际测试过这个,所以它可能有一些错误,但我认为基本想法是合理的。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)