2015-04-02 51 views
-1

我想复制一个表的记录,并想在另一个表中存储一些特定的记录。 #!的/ usr/bin中/ Python的如何复制一张表的数据并将其存储在另一张表中?

import sqlite3 

# Connect to the customer database and get the specific data 
connPublic = sqlite3.connect('customer.sqlite') 
cursorPublic = connPublic.cursor() 
data = cursorPublic.execute("SELECT * FROM connections WHERE connection_timestamp LIKE '%2015-03%' ") 

# Connect to the Company database to Insert specific records 
connLocal = sqlite3.connect('company.sqlite') 
print ("Open/Create Database Successfully"); 

# Create a database table for the connection if it is not exist 
connLocal.execute('''CREATE TABLE IF NOT EXISTS connections 
      (connection INTEGER PRIMARY KEY NOT NULL, 
      connection_type TEXT NOT NULL, 
      connection_transport NOT NULL, 
      connection_time TEXT NOT NULL);''') 

cursorLocal = connLocal.cursor() 

for i in data.fetchall(): 
    cursorLocal.execute("INSERT INTO connections (connection, connection_type, connection_transport, connection_time) 
    cursorLocal.execute("INSERT INTO connections (connection) VALUES VALUES (i[0], i[1], i[3], i[2]") 
connPublic.commit() 

但它抛出一个错误sqlite3.OperationalError: near "[0]": syntax error 我怎么可以这样操作。

回答

0

我想你的意思:

cursorLocal.execute("INSERT INTO connections (connection, connection_type, connection_transport, connection_time) VALUES (?, ?, ?, ?)", (i[0], i[1], i[3], i[2])) 

?字符为DB-API的参数替换。

+0

准确地说,我做了同样的工作:) – 2015-04-02 14:20:04

相关问题