Python database operations-PyMySQL entry PyMySQL is a module for operating MySQL in Python. it has the same basic functions as the MySQLdb module. PyMySQL has almost the same performance as MySQLdb.
It is not particularly strong. it will be more convenient to use PyMySQL. PyMySQL is completely written in python, avoiding the trouble of installing MySQLdb on different systems.
Applicable environment
Python version> = 2.6 or 3.3
Mysql version> = 4.1
Install
Run the following command on the command line:
pip install pymysql
Install it manually. download it first. : Https://github.com/pymysql/pymysql/tarball/pymysql-x.x.
X. X is the version.
Decompress the package. Enter the decompressed directory in the command line and execute the following command:
python setup.py install
We recommend that you use pip to install the package, which automatically solves the package dependency problem and avoids various errors during installation.
The basic operations of pymysql are as follows:
#! /Usr/bin/env python # -- coding = UTF-8 # Author Allen Leeimport pymysql # Create a link object conn = pymysql. connect (host = '2017. 0.0.1 ', port = 3306, user = 'root', passwd = '000000', db = 'Allen') # Create a cursor = conn. cursor () # execute SQL, update a single piece of data, and return the affected number of rows pait_row = cursor.exe cute ("update hosts set host = '1. 1.1.2 '") # insert multiple entries and return the affected function effect_row = cursor.exe cute.pdf (" insert into hosts (host, color_id) values (% s, % s )", [("1.0.0.1", 1,), ("10.0.0.3", 2)]) # obtain the latest auto-incrementing IDnew_id = cursor.lastrowid1_query data cursor.exe cute ("select * from hosts ") # obtain a row row_1 = cursor. fetchone () # get multiple (3) rows row_2 = cursor. fetchmany (3) # obtain all row_3 = cursor. fetchall () # reset the cursor type to dictionary type cursor = conn. cursor (cursor = pymysql. cursors. dictCursor) # submit and save the new or modified data conn. commit () # Close the cursor. close () # close the connection conn. close ()