Python3-mysql Adapter Pymysql

Source: Internet
Author: User
Tags mysql client python mysql git clone

In this paper, we introduce Python3 to use Pymysql Connection database, and realize simple and delete to change.

What is Pymysql?

Pymysql is a library used in the python3.x version to connect to the MySQL server, and MySQLdb is used in Python2.

Pymysql follows the Python database API v2.0 specification and includes the Pure-python MySQL client library.

Pymysql Installation

Before using Pymysql, we need to make sure that Pymysql is installed.

Pymysql:https://github.com/pymysql/pymysql.

If it is not installed, we can install the latest version of Pymysql using the following command:

Pymysql

If your system does not support the PIP command, you can install it in the following ways:

1. Download the installation package installation using the GIT command (you can also download it manually):

$ git clone https://github.com/pymysql/pymysqlpymysql/$ python3 setup.  PY Install      

2, the database operation instance, directly on the code.

ImportPymysqlImportDatainfoImport Time#Get ParametersHost=Datainfo.hostusername=Datainfo.usernamepassword=Datainfo.passworddatabase=datainfo.dbPrint()#Test Database Connectiondeftestconnect ():#Open Database LinkDB=Pymysql.connect (host,username,password,database)#Create a Cursor object using the cursor () method cursorcursor=db.cursor ()#Execute SQL query using the Execute () methodCursor.execute ("Select version ()")    #using Fetchone () to get a single piece of dataData=Cursor.fetchone ()Print(data) db.close ()#Insert Databasedefinsertdate ():#Open Database LinkDB= Pymysql.connect (host,username,password,database,charset='UTF8')    #Create a Cursor object using the cursor () method cursorcursor=db.cursor () create_time= Time.strftime ('%y-%m-%d%h:%m:%s') Update_time= Time.strftime ('%y-%m-%d%h:%m:%s') Start_time= Time.strftime ('%y-%m-%d%h:%m:%s') End_time= Time.strftime ('%y-%m-%d%h:%m:%s') Remark="Test Insert Information"    Print("Start")    #SQL INSERT statementsql ="INSERT INTO demo (Start_time,end_time,creat_time,update_time,remark)"           "VALUES ('%s ', ' %s ', '%s ', '%s ', '%s ')"          %(Start_time,end_time,create_time,update_time,remark)Try:        #Execute SQL        Print("Perform an Insert") TT=cursor.execute (SQL)Print(TT) Db.commit ()exceptUnicodeencodeerror as E:#Roll Back when an error occurs        Print(e) db.rollback () Db.close ( )#Query Operationsdefselectdata (): DB= Pymysql.connect (host, username, password, database, charset='UTF8')    #Create a Cursor object using the cursor () method cursorcursor=db.cursor () SQL="SELECT * FROM demo where ID >= '%d '"% (1)    Try:        #Execute SQL        Print("Execute Query") cursor.execute (SQL) Results=Cursor.fetchall () forRowinchResults:id=Row[0] Start_time= Row[1] End_time= Row[2] Create_time= Row[3] Update_time= Row[4] Remark= Row[5]            #Print Results            Print("id =%d,start_time=%s,end_time=%s,create_time=%s,update_time=%s,remark=%s"%(Id,start_time,end_time,create_time,update_time,remark)) Db.commit ()exceptUnicodeencodeerror as E:#Roll Back when an error occurs        Print(e) db.close ()#Update Actiondefupdate_data (): DB= Pymysql.connect (host, username, password, database, charset='UTF8')    #Create a Cursor object using the cursor () method cursorcursor=db.cursor () update_time= Time.strftime ('%y-%m-%d%h:%m:%s') SQL="Update demo Set update_time = '%s ' where ID >= '%d '"% (update_time,1)    Try:        #Execute SQL        Print("Perform the update") cursor.execute (SQL) Db.commit ()exceptUnicodeencodeerror as E:#Roll Back when an error occurs        Print(e) db.rollback () Db.close ( )#Delete Operationdefdelete_date (): DB= Pymysql.connect (host, username, password, database, charset='UTF8')    #Create a Cursor object using the cursor () method cursorcursor=db.cursor () SQL="Delete from demo where ID < '%d '"% (1)    Try:        #Execute SQL        Print("Perform the delete") cursor.execute (SQL) Db.commit ()exceptUnicodeencodeerror as E:#Roll Back when an error occurs        Print(e) db.rollback () Db.close ( )if __name__=='__main__': Testconnect () insertdate () Selectdata () Update_data () delete_date ()

Python3-mysql Adapter Pymysql

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.