首先使用连接到数据库sqlite3.connect,然后创建一个游标,以便执行sql.有了游标后,就可以执行任意的sql命令了.
例:
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()
Run Code Online (Sandbox Code Playgroud)
警告:我实际上没有测试过这个,所以它可能有一些错误,但我认为基本的想法是合理的.
| 归档时间: |
|
| 查看次数: |
5191 次 |
| 最近记录: |