How to use the pymysql library in Python to operate the Mysql database, just read this article~

 In order to make python connect to the database, you need a driver, which is a library for interacting with the database. This article introduces how to use the pymysql library in python to operate the mysql database.


1. What is pymysql?

pymysql is the interface to connect to the mysql database server from python. The simple understanding is that pymysql is a three-party module for python to operate the mysql database. You can connect to the database in python and write mysql commands. It is a pure python library.

2. Install pymysql

The pymysql module can be installed by the following command:

pip install pymysql

3. Connect to the database

After installing pymysql, you can use import pymysql to import modules in the python program.

Before connecting to the database, first make sure that the database server has been started. There is a created database in the database, here connect to the database 'mydb', the user name used is 'root', and the password is 'mysql'.

The more commonly used parameters include:

  • host: database host name. The default is to use the local host
  • user: database login name. The default is the current user
  • password: The secret of database login. The default is empty
  • database: The database name to use. There is no default value
  • port: The TCP port used by the MySQL service. The default is 3306
  • charset: database encoding
# import pymysql
import pymysql

# Open the database connection
conn = pymysql.connect(
    host = '127.0.0.1', # server address
    port = 3306, # server port
    user = 'root', # username		
    password = 'mysql', # password
    database = 'mydb', # the database to connect to
    charset = 'utf8' , # set database encoding
    cursorclass=pymysql.cursors.DictCursor # record results, dictionary display
)

# Use the cursor() method to create a cursor object cursor
cursor = conn.cursor()

# Close the database connection
conn.close()

In the above example, there is a cursor concept, which is equivalent to moving the cursor up and down, and each movement represents a record. In pymysql, the results returned by operations such as select can be read through the movement of the cursor and the corresponding method functions.

4. SQL injection

If some sql statements need to be spliced, you must use the execute() method provided by pymysql to splicing. Do not use the % or format() method in python, which may lead to sql injection problems and bring unsafe Hidden danger.

# import pymysql
import pymysql

# Open the database connection
conn = pymysql.connect(
    host = '127.0.0.1', # server address
    port = 3306, # server port
    user = 'root', # username		
    password = 'mysql', # password
    database = 'mydb', # the database to connect to
    charset = 'utf8' , # set database encoding
    cursorclass=pymysql.cursors.DictCursor # record results, dictionary display
)

# Use the cursor() method to create a cursor object cursor
cursor = conn.cursor()

# execute sql
sql = "select * from t1 where id=%s"
# Submit for execution, and return the number of rows affected by sql success. Splicing here can prevent sql injection problems
res = cursor.execute(sql,("1",))  

print(res) # 1 Find a record
# get all records
print(cursor.fetchall()) 

# close the cursor
cursor.close()  
conn.close()

5. Transaction submission

When performing operations such as update/insert/delete, you must use conn.commit() to commit the transaction to take effect. Or specify the auto_commit parameter as true when instantiating the conn object to automatically commit the transaction. When there is a problem with the operation, you can use conn.rollback() to roll back the transaction.

# import pymysql
import pymysql

# Open the database connection
conn = pymysql.connect(
    host = '127.0.0.1', # server address
    port = 3306, # server port
    user = 'root', # username		
    password = 'mysql', # password
    database = 'mydb', # the database to connect to
    charset = 'utf8' , # set database encoding
    cursorclass=pymysql.cursors.DictCursor, # record results, dictionary display
    autocommit = True # autocommit
)

# Use the cursor() method to create a cursor object cursor
cursor = conn.cursor()

# execute sql
sql = "insert into t1(name) values(%s)"
# Submit for execution, and return the number of rows affected by sql success. Splicing here can prevent sql injection problems
res = cursor.execute(sql,("new record",))  

print(res) # 1 successfully inserted a record
print(cursor.lastrowid) #Check after the insert statement, check the row number of the last record
print(cursor.fetchall())

# conn.commit() # Manual submission
cursor.close()
conn.close()

6. Submit multiple items

The cursor.executemany() method can submit multiple sql at one time.

# import pymysql
import pymysql

# Open the database connection
conn = pymysql.connect(
    host = '127.0.0.1', # server address
    port = 3306, # server port
    user = 'root', # username		
    password = 'mysql', # password
    database = 'mydb', # the database to connect to
    charset = 'utf8' , # set database encoding
    cursorclass=pymysql.cursors.DictCursor, # record results, dictionary display
    autocommit = True # autocommit
)

# Use the cursor() method to create a cursor object cursor
cursor = conn.cursor()

# execute sql
sql = "insert into t1(name) values(%s)" # Execute the same command 3 times
# Submit for execution, and return the number of rows affected by sql success. Splicing here can prevent sql injection problems
res = cursor.executemany(sql,[("New record 1"),("New record 2"),("New record 3")])  

print(res) # 3 successfully inserted three records
print(cursor.lastrowid) #Check after the insert statement, check the row number of the last record
print(cursor.fetchall())

cursor.close()
conn.close()

7. Cursor

After obtaining a record, we can control the movement of the cursor, and also control how many records are viewed after the cursor. Each movement of the cursor represents a record

Note: The number of records moved by the cursor is the number of records. If the movement value is negative N, it means the last N records

cursor.scroll(3,mode='absolute') The cursor moves backward 3 records with an absolute position
cursor.scroll(3,mode='relative') The cursor moves backward 3 records from the current position

If you want to get records, you can use the following three methods

cursor.fetchone() # Fetch the first record, the cursor moves down one row
cursor.fetchmany(2) # Fetch the next two records, the cursor moves down two rows
cursor.fetchall() # Fetch all records, move the cursor to the end, and return a list

8、DBUtils

Use the connection pool of DBUtils to establish a connection with the MySQL server, avoiding frequent socket connection requests, which is often used for concurrent access.

First, install it with the following command:

pip install dbutils

Instructions:

import pymysql
from dbutils.pooled_db import PooledDB

class MySqlPool(object):
    config = {
        "creator": pymysql, # use the module to connect to the database
        "host": "127.0.0.1",
        "port": 3306,
        "user": "root",
        "password": "mysql",
        "db": "mydb",
        "charset": "utf8",
        "autocommit": True,
        "mincached": 10, # The number of idle connections opened at startup (default value 0 does not create connections at the beginning)
        "maxconnections": 70, # The maximum number of connections in the connection pool
        "maxcached": 10, # The maximum number of idle connections allowed in the connection pool
        "maxshared": 10, # The maximum number of shared connections allowed (default value 0 means all connections are dedicated) If the maximum number is reached, the connection requested as shared will be shared
        "blocking": True, # Set the behavior when the connection pool reaches the maximum number (the default value 0 or False means return an error <toMany...> Other means block until the number of connections decreases and the connection is allocated)
        "maxusage": 0, # The maximum allowable multiplexing times of a single connection (the default value 0 or False represents unlimited multiplexing). When the maximum number is reached, the connection will automatically reconnect (close and reopen)
        "setsession": [], # An optional list of SQL commands used to prepare each session, such as ["set datestyle to german", ...], often used for initialization commands
        "cursorclass": pymysql.cursors.DictCursor # return type, dict
    }

    pool = PooledDB(**config) # Singleton mode creates a pool

    def get_db(self):
        self.conn = MySqlPool.pool.connection()
        self.cursor = self.conn.cursor()
        return self

    def close(self):
        self.cursor.close()
        self.conn.close()

    def __enter__(self):
        return self.get_db()

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.close()


# Use method one
with MySqlPool() as db:
    db.cursor.execute("select * from t1;")
    print(db.cursor.fetchall())

# Use method two
db = MySqlPool().get_db()
db.cursor.execute("insert into t1 (user_name,user_password,user_type) values ('aaa',123,1);")
print(db.cursor.fetchall())
db.close()

 

Guess you like

Origin blog.csdn.net/weixin_43805705/article/details/130869973