在MySQL中,我创建了一个名为"test“的db和一个名为" table”的表。表只有一个列名:"A“,并设置为datatype INT(1)。
在Python中,我有以下代码:
import MySQLdb
db = MySQLdb.connect(host="localhost",
user="root",
db="test")
cur = db.cursor()
myList = [1,2,3,4]
for row in myList:
print row
cur.execute("INSERT INTO table (a) VALUES (%s)" % row)上面代码的目标是在循环之后完成我的表“表”,应该有这样的数据:
|A|
|1|
|2|
|3|
|4|但是我的桌子在刷新后是空的。所以我的问题是如何在Python中插入到表from循环中。
Tnx
发布于 2015-06-30 13:16:46
你没有提交交易。尝试在循环后添加db.commit()。
发布于 2015-06-30 13:21:51
您可以在数据加载或预先设置db.commit ()之后使用db.autocommit(True)。
https://stackoverflow.com/questions/31139263
复制相似问题