制作脚本的最佳方法是使用CSV(带有n列)并将它们添加到sqlite数据库中?
这就是我现在所拥有的 -
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()
答案 0 :(得分:0)
显而易见的解决方案是使列数可配置(调用者应该知道,不是吗?)然后根据此信息构建插入语句。如果您不知道如何操作,请阅读FineManual关于字符串格式的部分。