Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

About MySQLdb conn.autocommit(True)

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

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

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

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

like image 291
chidan Avatar asked Aug 21 '12 16:08

chidan


People also ask

What is Conn autocommit?

When a connection is created, it is in auto-commit mode. This means that each individual SQL statement is treated as a transaction and is automatically committed right after it is executed. (To be more precise, the default is for a SQL statement to be committed when it is completed, not when it is executed.

What is the use of Conn COMMIT?

The commit() method of the Connection interface saves all the modifications made since the last commit. If any issue occurs after the commit you can revert all the changes done till this commit by invoking the rollback() method.

How do I set autocommit true in MySQL?

How do I set autocommit true in MySQL? Grouping DML Operations with Transactions To use multiple-statement transactions, switch autocommit off with the SQL statement SET autocommit = 0 and end each transaction with COMMIT or ROLLBACK as appropriate.

What do we need to check before connecting to Mysqldb?

For a client program to connect to the MySQL server, it must use the proper connection parameters, such as the name of the host where the server is running and the user name and password of your MySQL account.


2 Answers

by default MySQLdb autocommit is false,

You can set autocommit to True in your MySQLdb connection like this,

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()
like image 134
MikA Avatar answered Oct 14 '22 04:10

MikA


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.

like image 42
Colin Dunklau Avatar answered Oct 14 '22 04:10

Colin Dunklau