Python road [twenty-five]: Database of pymysql module

Advanced Database

A, pymysql module

pymysql in Python Mysql operation of the module, and the method of its use almost the same MySQLdb the py2.

Two, pymysql module mounting

pip install pymysql

Third, the implementation of sql statement

Coding _ * _ #: _ * _. 8 UTF- 
# the Author: Simon 
# Datetime: 2019/9/27 8:51 
# Software: PyCharm 

Import pymysql 

Conn = pymysql.connect (Host = '127.0.0.1', Port = 3306 , User = 'the root', the passwd = '123456', DB = 'lesson54' ) 

Cursor = conn.cursor () 

data results cursor = conn.cursor (cursor = pymysql.cursors.DictCursor) // # acquiring the data type changes The default is a tuple, can be changed dictionaries 

# = SQL "the CREATE TABLE the TEST (the INT ID, name VARCHAR (20 is))" 
# the cursor.execute (SQL) 
# the cursor.execute ( "the INSERT the INTO Test the VALUES (. 3, ' simon1 '), (4,' zhurui1 ') ") 

// query  row_affected = cursor.execute (" the SELECT * the FROM the Test " )  # = One cursor.fetchone ()  # = All the Cursor.fetchall() # many=cursor.fetchmany(2)
 Print (cursor.fetchone ()) Print (cursor.fetchall ()) Print (cursor.fetchmany ()) #scroll # cursor.scroll (-1, MODE = "relative") relative to the current position of the mobile # # cursor.scroll ( 1, mode = "absolute") # absolute relative position conn.commit () // executing the sql, first submitted cursor.close () // Close terminal # conn.close () // close the connection                

Fourth, the transaction

4.1 Transaction command

Transaction on only one set of logical operations, each unit consisting of the set of operations, either all succeed or all unsuccessful;

Open database transaction command

-         Start Transaction open affairs 
-         Rollback to roll back a transaction, ie revoke the designation of sql statement (only rollback insert delete update statements), commit to roll back to the previous position 
-         the Commit to commit the transaction, the transaction is committed not stored 
- - 
- that the savepoint retention point, the transaction set temporary placeholders you can publish it back-off (and the entire transaction rollback different)  

Transfer Example:

mysql> create table account(id int,name varchar(20),balance double);
Query OK, 0 rows affected (0.03 sec)

mysql> insert into test account values(1,"朱锐",16000);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'accou
nt values(1,"朱锐",16000)' at line 1
mysql> insert into account values(1,"朱锐",16000);
Query OK, 1 row affected (0.01 sec) mysql> insert into account values(2,"simon",46000); Query OK, 1 row affected (0.01 sec) mysql> select * from account; +------+--------+---------+ | id | name | balance | +------+--------+---------+ | 1 | 朱锐 | 16000 | | 2 | simon | 46000 | +------+--------+---------+ 2 rows in set (0.00 sec) mysql> start transaction; //开启事务 Query OK, 0 rows affected (0.00 sec) mysql> mysql> mysql> update account set balance=balance-5000 where id=1; //转账 Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from account; +------+--------+---------+ | id | name | balance | +------+--------+---------+ | 1 | 朱锐 | 11000 | | 2 | simon | 46000 | +------+--------+---------+ 2 rows in set (0.00 sec) mysql>

rollback rollback:

mysql> rollback;
Query OK, 0 rows affected (0.01 sec)

mysql> select * from account;
+------+--------+---------+
| id   | name   | balance |
+------+--------+---------+
|    1 | 朱锐   |   16000 |
|    2 | simon  |   46000 |
+------+--------+---------+
2 rows in set (0.00 sec)

mysql>

commit commit the transaction:

mysql> select * from account;
+------+--------+---------+
| id   | name   | balance |
+------+--------+---------+
|    1 | 朱锐   |   11000 |
|    2 | simon  |   46000 |
+------+--------+---------+
2 rows in set (0.00 sec)

mysql> update account set balance=balance+5000 where id=2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from account; +------+--------+---------+ | id | name | balance | +------+--------+---------+ | 1 | 朱锐 | 11000 | | 2 | simon | 51000 | +------+--------+---------+ 2 rows in set (0.00 sec) mysql> commit; Query OK, 0 rows affected (0.00 sec) mysql>

savepoint:

create table test2(id int PRIMARY KEY auto_increment,name VARCHAR(20)) engine=innodb;
INSERT INTO test2(name) VALUE ("simon"),
                              ("zhurui"),
                              ("caiyunjie");



start transaction;
insert into test2 (name)values('zhuruirui');
select * from test2; commit; -- 保留点 start transaction; insert into test2 (name)values('huozhu'); savepoint insert_wu; select * from test2; delete from test2 where id=4; savepoint delete1; select * from test2; delete from test2 where id=1; savepoint delete2; select * from test2; rollback to delete1; select * from test2; savepoint

Call database that initiated the transaction 4.2 python in the way

import pymysql

#添加数据

conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='test')

cursor = conn.cursor()


try:
    insertSQL0="INSERT INTO ACCOUNT2 (name,balance) VALUES ('caiyunjie',60000)" insertSQL1="UPDATE account2 set balance=balance-12700 WHERE name='simon'" insertSQL2="UPDATE account2 set balance=balance+12700 WHERE name='zhurui'" cursor = conn.cursor() cursor.execute(insertSQL0) conn.commit() cursor.execute(insertSQL1) raise Exception cursor.execute(insertSQL2) cursor.close() conn.commit() except Exception as e: conn.rollback() conn.commit() cursor.close() conn.close()

Guess you like

Origin www.cnblogs.com/hackerer/p/11596618.html