スクリプトが CSV (n 列) を取得し、それらを sqlite db に追加する最良の方法は何でしょうか?
これは私が今まで持っているものです -
def export_to_db():
conn = sqlite3.connect(config.DB_NAME)
cur = conn.cursor()
table_name = config.TABLE_NAME
with open(config.IMPORT_FILENAME, 'rb') as csvfile:
tablerows = csv.reader(csvfile, delimiter=',')
header = True # make this configurable
for row in tablerows:
if header:
header = False
else:
cur.execute("INSERT INTO <tablename> VALUES(?, ?, ?, ?)", tuple(row))
# I want the number of columns to be same as teh number of columns in CSV.
conn.commit()
cur.execute("SELECT COUNT(*) from people")
print str(cur.fetchone()[0]) + " rows added!"
conn.close()