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