python3.2.5 + PyMySQL3 0.5 Basic use method

Source: Internet
Author: User

Many articles on the internet have chosen mysqldb as a tool for Python to connect to MySQL database, but MYSQLDB currently supports a lower version of Python and failed to install. So just try Pymysql, this is easier to use

Software:

python3.2.5:https://www.python.org/ftp/python/3.2.5/python-3.2.5.msi

pymysql3.0.5: https://pypi.python.org/packages/source/P/PyMySQL3/PyMySQL3-0.5.tar.gz

MySQL:http://www.phpstudy.net/phpstudy/phpStudyLite.zip (for easy installation, I choose Phpstudy here)

1. The Python installation directory is set to D:\python32

2, Pymysql installation method is: Unzip the downloaded file, run in cmd: Python setup.py install.

Method for verifying the success of installation installation: Import Pymysql. If not the error indicates the installation was successful.

3, MySQL installation directory for D:\phpStudy\MySQL. To avoid more configuration problems, you can set it to system service after you start Phpstudy

4. Basic Operation:

(1) Import Pymysql:import Pymysql

(2) Connection database: Conn=pymysql.connect (host= ' localhost ', user= ' root ', passwd= ' root ', db= ' ere ', charset= ' UTF8 ') Be sure to pay attention to the contents before the equals sign! CharSet parameter to avoid Chinese garbled characters

(3) Get operation cursor:cur=conn.cursor ()

(4) Execute SQL statement, insert record: Sta=cur.execute ("INSERT statement") After successful execution, STA value is 1. The update, DELETE statement is similar to this.

(5) Execute SQL statement, query record: Cur.execute ("SELECT statement") after successful execution, the query result recordset is saved in the CUR variable, and then the result is printed in a loop:

For each in cur:

Print (Each[1].decode (' Utf-8 ') # Each[1] represents the 2nd column value of the row that the current cursor is in, and if it is in Chinese it needs to process the encoding

(6) Close database connection: Cur.close (); Conn.close ();

A complete case:

Import pymysql from builtins import Intdef conndb (): #连接数据库函数 conn=pymysql.connect (host= ' localhost ', user= ' root ', passwd    = ' 123 ', db= ' ere ', charset= ' UTF8 ') cur=conn.cursor ();    Return (conn,cur);d EF exeupdate (cur,sql): #更新语句, can execute Update,insert statement sta=cur.execute (SQL); Return (STA);d EF exedelete (cur,ids): #删除语句, can be deleted in bulk for Eachid in Ids.split ("): Sta=cur.execute (' Delete from Relat    Iontriple where TID =%d '% int (eachid));    Return (STA);d EF exequery (cur,sql): #查询语句 cur.execute (SQL);    return (cur);d ef connclose (conn,cur): #关闭所有连接 cur.close ();    Conn.close (); #调用连接数据库的函数 conn,cur=conndb (); #调用更新记录的函数sta =exeupdate (cur, "insert into relationtriple values (null, ' A ', ' B ', ' nickname ', ' None ')    ), if (sta==1): print (' Insert succeeded '); Else:print (' Insert failed ');     #查询现有数据, and print exequery (cur, "select * from Relationtriple"), and for each in Cur:print (Each[0],each[1].decode (' utf-8 ')); # bulk Delete records, user input to delete the record ID number tempid=input (' Please enter the number to be removed separated by a space: '); Sta=exedelete (cur, tempid); if (sta==1): PrinT (' delete succeeded '); Else:print (' delete failed '); Connclose (conn, cur);     


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.