[Python] Python operation MySQL

Source: Internet
Author: User

"Install"

Install MySQL

Install MySQL Needless to say, download the installation is, there is no special need to pay attention to the place.

One: Click to open the link

"Examples"

# coding=utf-8import mysqldb# Query number def Count (cur): Count=cur.execute (' select * from Student ') print ' There have%s rows R Ecord '% count #插入def insert (cur): sql = "INSERT into Student (id,name,age,sex) VALUES (%s,%s,%s,%s)" param = (2, ' Xiaom ing ', ' boy ') Cur.execute (sql,param) #查询 def Select (cur): n = Cur.execute ("SELECT * from Student") print "--- ---"For row in Cur.fetchall (): for R in row:print R print"------"#更新def Update (cur): SQL = "Update Student set Name =%s where ID = 2" param = ("Xiaoxue") Count = Cur.execute (sql,param) #删除def Delete (c   UR): sql = "Delete from Student where Name =%s" param = ("Xiaoxue") n = cur.execute (Sql,param) Try:   Conn=mysqldb.connect (host= ' localhost ', user= ' root ', passwd= ' 123456 ', db= ' python ', port=3306) cur=conn.cursor () #数量   Count (cur) #查询 Select (cur) #插入 insert (cur) print "After Insert" #查询 Select (cur) #更新 update (cur) print "updated" #查询 Select (cur) #删除 DeLete (cur) print "deleted" #查询 Select (cur) cur.close () conn.close () except Mysqldb.error,e:print "Mysql Erro R%d:%s "% (E.args[0], e.args[1])








[Python] Python operation MySQL

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.