Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python MySQLdb executemany不能添加数据_Python_Mysql_Mysql Python - Fatal编程技术网

Python MySQLdb executemany不能添加数据

Python MySQLdb executemany不能添加数据,python,mysql,mysql-python,Python,Mysql,Mysql Python,我尝试使用MySQLdb executemany命令将数据添加到表(test_copy),如下所示: db = mdb.connect(host="127.0.0.1",port=3306,user='XXXX',db="test") cursor = db.cursor() COM = "insert into test_copy (Short_Model) VALUES (%s)" VALS = ['213','3232','fd','sadad'] cursor.executemany(C

我尝试使用MySQLdb executemany命令将数据添加到表(test_copy),如下所示:

db = mdb.connect(host="127.0.0.1",port=3306,user='XXXX',db="test")
cursor = db.cursor()
COM = "insert into test_copy (Short_Model) VALUES (%s)"
VALS = ['213','3232','fd','sadad']
cursor.executemany(COM,VALS)
cursor.close
注:表名=测试副本,列名=短模型

问题是命令运行时没有任何错误,但当我检查表时,没有添加任何数据

如果这是一个简单的问题,我很抱歉,但在过去的几个小时里,这让我发疯


谢谢。

cursor.close
应该是
cursor.close()
,但是您通常希望
cursor.commit()
只是为了确保您的更改反映在数据库中。

需要调用
commit()
来完成数据库中的事情。文档没有说明
executemany
是否自动提交更改。同时打开自动提交,
cursor.autocommit(True)
可能会有帮助。

谢谢@Jon Clements和@Abhishek Mishra-你让我恢复了理智。以下是感兴趣人士的最终解决方案:

db = mdb.connect(host="127.0.0.1",port=3306,user='xxxx',db="test")
cursor = db.cursor()
COM = "insert into test_copy (Short_Model) VALUES (%s)"
VALS = ['213','3232','fd','sadad']
cursor.executemany(COM,VALS)
db.commit()

根据我的经验,
executemany
会自动提交,尽管我希望它不会。