[Python] Python operation MySQL

Source: Internet
Author: User

Installation

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"

?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 # coding=utf-8import MySQLdb#查询数量def Count(cur):   count=cur.execute(‘select * from Student‘)   print ‘there has %s rows record‘ % count   #插入def Insert(cur):   sql = "insert into Student(ID,Name,Age,Sex)values(%s,%s,%s,%s)"   param = (2,‘xiaoming‘,24,‘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(cur):       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 "插入之后"   #查询   Select(cur)   #更新   Update(cur)   print "更新之后"   #查询   Select(cur)   #删除   Delete(cur)   print "删除之后"   #查询   Select(cur)      cur.close()   conn.close()   except MySQLdb.Error,e:   print "Mysql Error %d: %s" % (e.args[0], e.args[1])


[Python] Python operation MySQL

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.