まず、Python ビット。my_arr
それがある種の 2 次元配列であり、リストのリストを生成すると仮定すると.tolist()
、はい、リストのすべての行に要素を追加する方法があります。
result = [[a]+b for a,b in zip(TableNameList, my_arr.tolist()]
次に、SQL ビットです。?
いいえ、 を使用してテーブル名を指定することはできません。テーブル名は、SQL ステートメントに文字通り存在する必要があります。私が提供できる最善の方法は、curssor.execute
数回実行することです。
for table, values in zip(TableNameList, my_arr):
c.execute("INSERT INTO %s VALUES (?, ?, ?)"%table, values)
ただし、のソースを信頼するかどうかに注意してくださいTableNameList
。信頼できないデータを使用すると、%s
SQL インジェクションのセキュリティ上の欠陥につながります。
サンプルプログラム:
import sqlite3
import numpy as np
import itertools
my_arr = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
TableNameList = 't1', 't1', 't2', 't3'
conn = sqlite3.connect(':memory:')
c = conn.cursor()
c.execute('''CREATE TABLE t1 (c1, c2, c3)''')
c.execute('''CREATE TABLE t2 (c1, c2, c3)''')
c.execute('''CREATE TABLE t3 (c1, c2, c3)''')
## Insert a row of data
#c.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)")
for table, values in itertools.izip(TableNameList, my_arr):
c.execute("INSERT INTO %s VALUES (?, ?, ?)"%table, values)
# Save (commit) the changes
conn.commit()
# We can also close the connection if we are done with it.
# Just be sure any changes have been committed or they will be lost.
conn.close()