[Python] Using Python for MySQL
[Installation]
Install MySQL
You don't need to talk about installing MySQL. download and install MySQL without special attention.
One: Click to open the link
[Example]
# Coding = utf-8import MySQLdb # number of queries def Count (cur): countw.cur.exe cute ('select * from Student ') print 'there has % s rows record '% count # Insert def insert (cur): SQL = "Insert into Student (ID, Name, Age, Sex) values (% s, % s, % s, % s) "param = (2, 'xiaoming', 24, 'boys') cur.exe cute (SQL, param) # query def Select (cur ): n = cur.exe cute ("select * from Student") print "------" for row in cur. fetchall (): for r in row: print r print "------" # Update def Update (cur ): SQL = "update Student set Name = % s where ID = 2" param = ("xiaoxue") count = cur.exe cute (SQL, param) # Delete def Delete (cur ): SQL = "delete from Student where Name = % s" param = ("xiaoxue") n = cur.exe cute (SQL, param) try: conn = MySQLdb. connect (host = 'localhost', user = 'root', passwd = '000000', db = 'python', port = 123456) cur = conn. cursor () # Count (cur) # query Select (cur) # Insert (cur) print "after insertion" # query Select (cur) # Update (cur) print "updated" # query Select (cur) # Delete (cur) print "after deletion" # query Select (cur) cur. close () conn. close () distinct T MySQLdb. error, e: print "Mysql Error % d: % s" % (e. args [0], e. args [1])