[Python] Python for MySQL and 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]
?
| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
# coding=utf-8import MySQLdb # Query quantitydef Count(cur): count=cur.execute('select * from Student') print 'there has %s rows record' % count # Insertdef Insert(cur): sql = "insert into Student(ID,Name,Age,Sex)values(%s,%s,%s,%s)" param = (2,'xiaoming',24,'boy') cur.execute(sql,param) # Querydef Select(cur): n = cur.execute("select * from Student") print "------" for row in cur.fetchall(): for r in row: print r print "------"# Updatedef Update(cur): sql = "update Student set Name = %s where ID = 2" param = ("xiaoxue") count = cur.execute(sql,param) # Deletedef 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() # Quantity Count(cur) # Query Select(cur) # Insert Insert(cur) print "After insertion" # Query Select(cur) # Update Update(cur) print "Updated" # Query Select(cur) # Delete Delete(cur) print "After deletion" # Query Select(cur) cur.close() conn.close() except MySQLdb.Error,e: print "Mysql Error %d: %s" % (e.args[0], e.args[1]) |