MySQL client does not need to commit code needs to commit cause analysis

A client with python difference in the operation of the database

In everyday use mysql client operations using python and mysql-source operating mysql, they will find inconsistencies to put:

In the mysql client, select whether to perform or execute commands such as delete, you do not need to commit; but we often write python code conn.commit () code, etc.

In the mysql client, deleted data is deleted, though we often say that the rollback command but the command line does not seem to be directly rolled back; but we often have python code execution if sql language Exception caught, use conn.rollback () rollback code.

 

Second, from the packet analysis underlying reason for the difference of the two

First, we must clear two things: First, the client and server operating python code is the same, so the reasons for the differences can not be caused by a server; and second, he can only receive from the server perspective to sent packet, and we can not know that this packet is sent by the client or python sent.

So that is the reason the client and python there are differences in the operation of the database, and can only be a client to the server and the python made a different data packets caused.

 

2.1 Environment Preparation

Current table following two records, we were used to delete the client record id is 0, the Python code deletes a record id to see any difference between the two data packets transmitted.

 

2.2 When the client-side interaction delete data packet

Client: workbench.

Note: You can also install the windows mysql then be used directly mysql.exe, but be careful not to ssh to the server and log database, this package can only be caught ssh packets can not find the original mysql protocol packets.

 

 Packet interaction is as follows. The client sends a delete command to the server directly delete success:

 

2.3 python delete the data exchange process

Test code is as follows:

import pymysql

def init_db(host="192.168.220.128",user="root",passwd="toor",db="test_db",charset="utf8"):
    try:
        conn = pymysql.connect(host, user, passwd, db, charset=charset)
    except Exception as e:
        print(e)
        exit(1)
    return conn

def delete_record(conn):
    cursor = conn.cursor()
    sql = "delete from test_table1 where id = 1"
    cursor.execute(sql)
    try:
        conn.commit()
    except:
        conn.rollback()

conn = init_db()
delete_record(conn)

Packet interaction is as follows. First, when connecting the database, the frame is automatically sent a "SET AUTOCOMMIT = 0" command to the server

 Next python side and then sends a command to delete a client like:

 

 Finally, give the server sends "COMMIT" command in python commit:

 

 

2.4 summary

Aggregate data above two subsections. The client sends the command as follows:

delete from test_table1 where id = 0

python code transmission command as follows:

SET AUTOCOMMIT = 0
delete from test_table1 where id = 0
COMMIT

很容易可以得出结论:python代码之所以相较客户端需要commit,是因为python在连接数据库时禁用了自动确认(SET AUTOCOMMIT = 0)。

另外我们不难得到一个推论:如果客户端登录后,用户敲击“SET AUTOCOMMIT = 0”,那么其后续在客户端中对服务端的修改操作,将和python代码一样需要COMMIT、及如果删完数据想反悔将可以ROLLBACK。

Guess you like

Origin www.cnblogs.com/lsdb/p/12155527.html
Recommended