关于MySQLdb conn.autocommit(True) [英] About MySQLdb conn.autocommit(True)

查看:297
本文介绍了关于MySQLdb conn.autocommit(True)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装了python 2.7 64bit,MySQL-python-1.2.3.win-amd64-py2.7.exe.

I have installed python 2.7 64bit,MySQL-python-1.2.3.win-amd64-py2.7.exe.

我使用以下代码插入数据:

I use the following code to insert data :

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客户端数据中不可见.

This results in printing n as 1, but in mysql client data aren't visible.

google说我必须添加conn.autocommit(True).

google says I must add conn.autocommit(True).

但是我不知道为什么MySQLdb将其关闭;

but I don't know why MySQLdb turns it off;

推荐答案

我不知道是否有特定的原因将自动提交与GAE一起使用(假设您正在使用它).否则,您可以手动提交.

I don't know if there's a specific reason to use autocommit with GAE (assuming you are using it). Otherwise, you can just manually commit.

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('/')

请注意,您可能应该检查插入是否成功完成,否则,请回滚提交.

Note that you probably should check if the insert happened successfully, and if not, rollback the commit.

这篇关于关于MySQLdb conn.autocommit(True)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆