Python3 using Pymysql to connect to MySQL database

Source: Internet
Author: User

One: Pymysql package installation (Window System under Python3)

Easy_install Pymysql

Two: MySQL and python interactive use

1 ImportPymysql2 Try:3     #get a database connection4conn = Pymysql.connect (host='localhost', user='Root', port=3306,passwd='Root', db='QE', charset='UTF8')5     #create an object to execute the SQL statement6Cur =conn.cursor ()7Cur.execute ('SELECT * FROM Students')8     #data = Cur.fetchone () #执行sql语句时获取第一行数据9data = Cur.fetchall ()#when you execute an SQL statement, you get all the rows, one row constitutes a tuple, and the elements are assembled into a tuple to returnTen      forDinchData: One         Print(d) ACur.close ()#g Close Object -Conn.close ()#Close Connection - exceptException:Print('Query failed')

To work hard, insist!! 2018-03-28

Python3 using Pymysql to connect to MySQL database

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.