我已经安装了python 2.7 64位,MySQL-python-1.2.3.win-amd64-py2.7.exe。
我使用以下代码来插入数据:
class postcon:
def POST(self):
conn=MySQLdb.connect(host="localhost",user="root",passwd="mysql",db="dang",charset="utf8")
cursor = conn.cursor()
n = cursor.execute("insert into d_message (mid,title,content,image) values(2,'xx','ccc','fff')")
cursor.close()
conn.close()
if n:
raise web.seeother('/')这会导致将n打印为1,但在mysql客户端中数据是不可见的。
谷歌说我必须添加conn.autocommit(True)。
但是我不知道为什么MySQLdb把它关掉了;
发布于 2012-08-22 00:45:57
我不知道在GAE中使用自动提交是否有特定的原因(假设您正在使用它)。否则,您可以手动提交。
class postcon:
def POST(self):
conn=MySQLdb.connect(host="localhost",user="root",passwd="mysql",db="dang",charset="utf8")
cursor = conn.cursor()
n = cursor.execute("insert into d_message (mid,title,content,image) values(2,'xx','ccc','fff')")
conn.commit() # This right here
cursor.close()
conn.close()
if n:
raise web.seeother('/')请注意,您可能应该检查插入是否成功,如果没有,则回滚提交。
发布于 2017-07-10 17:41:01
默认情况下,MySQLdb自动提交为false,
您可以将MySQLdb连接中的自动提交设置为True,如下所示:
conn=MySQLdb.connect(host="localhost",user="root",passwd="mysql",db="dang",charset="utf8")
conn.get_autocommit() #will return **False**
conn.autocommit(True)
conn.get_autocommit() #Should return **True** now
cursor = conn.cursor()发布于 2021-01-22 03:19:39
Connector/Python Connection Arguments
当您连接到数据库时,可以直接打开自动提交:
import mysql.connector as db
conn = db.connect(host="localhost", user="root", passwd="pass", db="dbname", autocommit=True)或
import mysql.connector
db = mysql.connector.connect(option_files='my.conf', autocommit=True)或者在调用close之前调用conn.commit()。
https://stackoverflow.com/questions/12059424
复制相似问题