[Python] Using Python for MySQL

Source: Internet
Author: User

[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])








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.