:Inserting multiple rows in a single SQL query?、一つの方法はあなたのコマンドに
contactlist = [
['Siemens, Harper', '323-4149'],
['Smith, Patti', '239-1212'],
['Jackson, Janet', '313-1352'],
['Manfredi, Ralph','872-2221'],
['Thompson, Bobby', '365-2622'],
['James, Lebron', '457-6223'],
['Ziegler, Zig', '667-1101'],
['Robbins, Tony', '329-2310']
]
str([tuple(i) for i in contactlist])[1:-1]
プリント
"('Siemens, Harper', '323-4149'), ('Smith, Patti', '239-1212'), ('Jackson, Janet', '313-1352'), ('Manfredi, Ralph', '872-2221'), ('Thompson, Bobby', '365-2622'), ('James, Lebron', '457-6223'), ('Ziegler, Zig', '667-1101'), ('Robbins, Tony', '329-2310')"
しかし、必要はありませんPythonのドキュメントを見ての挿入文字列を作成することです。この 完全な例を考えてみましょう:(https://docs.python.org/2/library/sqlite3.html):
import sqlite3
contactlist = [
['Siemens, Harper', '323-4149'],
['Smith, Patti', '239-1212'],
['Jackson, Janet', '313-1352'],
['Manfredi, Ralph','872-2221'],
['Thompson, Bobby', '365-2622'],
['James, Lebron', '457-6223'],
['Ziegler, Zig', '667-1101'],
['Robbins, Tony', '329-2310']
]
conn = sqlite3.connect('example.db')
c = conn.cursor()
# Delete table
c.execute('drop table if exists employees')
# Create table
c.execute('''CREATE TABLE employees
(name, number)''')
# Insert a row of data
c.executemany('INSERT INTO employees VALUES (?,?)', contactlist)
# or
# c.execute("INSERT INTO employees VALUES {}".format(str([tuple(i) for i in contactlist])[1:-1]))
conn.commit() # Commit the changes
conn.close()
データベースは現在、次の行を持つ従業員と呼ばれるテーブルが含まれています:ただの提案
('Siemens, Harper', '323-4149')
('Smith, Patti', '239-1212')
('Jackson, Janet', '313-1352')
('Manfredi, Ralph', '872-2221')
('Thompson, Bobby', '365-2622')
('James, Lebron', '457-6223')
('Ziegler, Zig', '667-1101')
('Robbins, Tony', '329-2310')
可能な重複Python](https://stackoverflow.com/questions/952914/making-a-flat-list-ouリストの中のリストのリスト) – Turn
あなたは 'db'を参照していますか? – Rahul