将特定列从一个数据库导出到另一个数据库

时间:2015-11-17 12:31:18

标签: python sqlite

我想使用Python将特定列从一个数据库导出到另一个数据库,但它不会出现:

# Display all Non-Duplicate data
import sqlite3
import csv

conn = sqlite3.connect('data.db')
# STEP 2 : create a small data file with only three fields account_id, product_id and unit_quantity
cursor = conn.execute("SELECT field1,field12,field14 FROM database")

for row in cursor:
    print row[0:11]     

print "Operation done successfully";
conn.close()

1 个答案:

答案 0 :(得分:0)

创建第二个连接并直接插入

conn = sqlite3.connect('data.db')
cursor = conn.execute("SELECT field1,field12,field14 FROM database")

export = sqlite3.connect('exported.db')
#get result as list
for values in cursor.fetchall():
    export.execute('INSERT INTO tablename(field1,field12,field14) VALUES (%s, %s, %s)' % (values[0], values[1], values[2]))
export.commit()
export.close()