Database operations using pymysql

Learn how to use the pymysql python module to operate mysql database

Here's the basic usage of the main draws of the blog entry: https://www.cnblogs.com/woider/p/5926744.html

The authors concluded that because of this very comprehensive, very simple, very easy to read

The main method of a .pymysql

Copy the code
pymysql.connect () Parameter Description: (connecting to the database parameters need to be added) 
Host (str): MySQL server address 
port (int): MySQL server port number 
user (str): Username 
passwd (str): password 
db (str ): database name 
charset (str): coded connection connect () object supports methods: 
cursor () using the connection creates and returns the cursor 
commit () Commits the current transaction 
rollback () to roll back the current transaction 
close () closes the connection cursor object supports method: 
the execute (OP) to perform a database query command 
fetchone () get the next row of the result set 
fetchmany (size) Gets the next few lines of the result set 
fetchall () Gets all the rows in the result set 
rowcount () returns the number of pieces of data or affect the number of rows 
close () the cursor objects



Copy the code

II. Common Operations

1. query data

Copy the code
Coding #:. 8 UTF- 
# author: HMK 

Import pymysql.cursors 

# database connection 
Conn = pymysql.connect (Host = 'localhost', 
                       User = 'the root', 
                       password = '123456', 
                       DB = 'Test', 
                       charset = ' utf8 ') 

# create a cursor 
the cursor = conn.cursor () 

# query data 
SQL = "the SELECT * from maoyan_movie" 
cursor.execute (SQL) # execute SQL 

# query all data and returns the results to the default tuples form, so it can be iterative processing 
for I in cursor.fetchall (): 
    Print (I) 
Print ( 'query to co:' 'of data', cursor.rowcount,) 

# acquiring a first data row 
result_1 = cursor.fetchone () 
Print (result_1 )
 
# get the first n rows of data
= cursor.fetchmany result_3 (. 3) 
Print (result_3) 

cursor.close () # close the cursor
conn.Close () # close the connection
Copy the code

2. Insert Data

Copy the code
Coding #:. 8 UTF- 
# author: HMK

Import pymysql.cursors

# database connection
Conn = pymysql.connect (Host = 'localhost',
User = 'the root',
password = '123456',
DB = 'Test',
charset = ' utf8 ')

# create a cursor
cursor = conn.cursor ()
# Insert data 
# data directly written in the sql later 
sql = "insert into maoyan_movie (ranking , movie, release_time, score) values (% s,% s,% s,% s)" # note the% s, not S% 
Cursor .execute (sql, [ '101' , ' top players', 'release Date: 2018-05-21', '9.2']) # list format data 
cursor.execute (sql, ( '102' , ' Mario' , 'release Date: 2018-01-21', '9.2')) tuple format data # 

# separate data into an object 
sql = "insert into maoyan_movie values ( % s,% s,% s,% s)" 
data = ( '102', 'ferritin Rider', 'release Date: 2019-01-21', '9.5') 
in between cursor.execute (sql, data) #sql and data "," separated
= SQL "INSERT INTO maoyan_movie values (% S, '% S', '% S', S%)" 
Data = (102, 'ferritin Rider', 'Release Date: 2019-01-21', 9.5)
the cursor.execute (sql% data) between data #sql and "%" are spaced apart, in which case the same attention sql Chinese characters give the corresponding placeholder quotes, i.e., "% S", or will be error : Unsupported format Character  

conn.commit () # submit, or insert can not be saved or modified data ( this must not forget to add )
cursor.close () # close the cursor 
conn.Close () # close the connection
Copy the code

 3. Modify the data

Copy the code
# coding: utf-8
# author: hmk

import pymysql.cursors

# 连接数据库
conn = pymysql.connect(host='localhost',
                       user='root',
                       password='123456',
                       db='test',
                       charset='utf8')
# Create a cursor 
cursor = conn.cursor ()
# Modify data 
sql = "update maoyan_movie set movie = '% s' where ranking =% s" # % s attention when quotes, when without 
data = ( 'rejuvenated',. 1) 
the cursor.execute (SQL Data% ) 

SQL = "Update maoyan_movie Movie SET% =% S = S WHERE Ranking" 
Data = ( 'silent land',. 1) 
the cursor.execute (SQL, Data)
conn.commit () # submit, or insert can not be saved or modified data
cursor.close () # close the cursor 
conn.Close () # close the connection
Copy the code

4. Delete Data

Copy the code
Coding #:. 8 UTF- 
# author: HMK 

Import pymysql.cursors 

# database connection 
Conn = pymysql.connect (Host = 'localhost', 
                       User = 'the root', 
                       password = '123456', 
                       DB = 'Test', 
                       charset = ' UTF8 ') 

# create a cursor 
cursor = conn.cursor () 

# delete data 
SQL = "delete from maoyan_movie% S = WHERE Ranking" 
data = (2) 
the cursor.execute (SQL, data) 

conn.commit () # submitted, otherwise, the deletion will not take effect
cursor.close () # close the cursor 
conn.Close () # close the connection
Copy the code

Use the attached table:

Copy the code
create table `maoyan_movie` (
    `ranking` double ,
    `movie` varchar (150),
    `release_time` blob ,
    `score` float 
); 
Copy the code

Guess you like

Origin www.cnblogs.com/cangqinglang/p/11353554.html