Link: https://pan.baidu.com/s/1Qq3FAqzkmakso2E7KuxvVg extraction code: KCZQ author - \ / 307570512Copy the code

Introduction to the three major databases operated by Python

MySQL is one of the best RDBMS(Relational Database Management System) applications in WEB applications. MongoDB MongoDB and Redis are both NoSQL (non-relational databases) and use structured data stores. There are some differences in the usage scenarios of the two, mainly due to the differences in the processing process of memory mapping and persistence. MongoDB recommends cluster deployment. Redis is more focused on sequential process writes. Although cluster is supported, it is limited to the master-slave mode. Redis Redis is an open source, network-enabled, memory-based, persistent, logging, key-value database that is written in ANSI C and complies with the BSD protocol. It is often called a data structure server because values can be of the types String, hash, list, set, and sorted sets.

Pythonr operates the three major databases of NetEase news client

Process: Start – Create connection- obtain cursor-(execute query, command, obtain, process data)- close cursor- Close connection- End

Install the Pymysql module

pip install pymysql
Copy the code

Importing the module

import pymysql

print(pymysql.version)
Copy the code

Database connection object Connection

Pymysql. Connect (parameters) : This object supports the following methods: Commit () commits the current transaction rollback() Rolls back the current transaction Close () closes the connection pymysql.connect(host=’localhost’,port=3306,user=’root’,passwd=’root’,db=’mysql’,charset=’utf8′)

Database interaction object CURSOR

This object supports the following methods: Fetchmany (size) Fetchall () fetchall() fetchall() fetchall() fetchall() Number of rows returned by the last execute or the number of affected rows Close () Closes the object

Note: After the execute method is called, the data is stored in the buffer. The fetch method is called to fetch the data

Fetchall (‘select * from myapp_student’) print(cursor.rowcount) for I in cursor.fetchall(): print(I)

The transaction

Atomicity: all or none of the operations in a transaction are consistent: the transaction must ensure the persistence of the database’s transition from one consistent state to another: once a transaction is committed, the change to the database is permanent isolation: the execution of a transaction cannot be interfered with by other transactions

Auto-commit is generally set to invalid during development

Disable autocommit,conn.autocommit(False), normal end: conn.mit (), exception end: conn.rollback(), the pymysql module defaults to False

try: Cursor. execute('insert into myapp_student values (11,"test",20,1,"haha",1)') cursor.execute('insert into myapp_student Values (13,"test",20,1,"haha",1) cursor.execute('delete from myapp_student where qid=1000') conn.mit () except: Print ('SQL statement error, execute rollback ') conn.rollback()Copy the code

.

.