Code for connecting python to MySQL

Source: Internet
Author: User
I used python to operate the MySQL database a few days ago and found it very interesting. In addition, the python Method for operating MySQL is very simple and fast. Code I would like to share with you the hope to help you learn. Import sys
Import mysqldb

Reload (sys)
SYS. setdefaultencoding ('Utf-8')

Def Getdata ():
Try :
Conn = mysqldb. Connect (host = 'Localhost' , User = 'Root' , Passwd = 'Root' , DB = 'Test' , Port = 3306 , Charset = 'Utf8' )
Try :
Cur = conn. cursor ()
SQL = R 'select * From person'
Cur.exe cute (SQL)
Allperson = cur. fetchall ()
Finally :
Cur. Close ()
Conn. Close ()
Except Exception, E:
Print 'Database error :' , E
Return

ForRECInAllperson:
PrintREC [0], REC [1]

If_ Name _ ='_ Main __':
Getdata () friends, remember to save it! Next time, you may need to use python to operate MySQL databases. If you use Django for development, you can check the database operations. Django SQLite database configuration problems originalArticlePlease indicate Reprinted from Old Wang python, this article address: http://www.cnpythoner.com/post/149.html

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.