Python3 connecting MySQL DB instance

Source: Internet
Author: User

#python3 connecting MySQL instancesImportPymysql"""Import connection MySQL required package, do not install pymysql need to install using command line switch to python installation path under Scripts subdirectory installation (pip install pymysql)"""#connect to MySQL databasedb = Pymysql.connect ("localhost","Root","123456","Python_conn", CharSet ='UTF8')"""five parameters for the host name, user name, password, the database name to connect to, in order to solve the problem of Chinese characters character set"""#get an operation cursor using the cursor () methodcursor =db.cursor ()#executing SQL statements using the Execute () methodsql ='SELECT * from Test_conn'cursor.execute (SQL)#get all data using the Fetchone () methodResults =Cursor.fetchall ()#Print forRowinchResults:sid=Row[0] Name= Row[1] Age= Row[2] Sclass= Row[3]    Print(Sid,name,age,sclass)#Close the databaseDb.close ()

Python3 connecting MySQL DB instance

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.