sudo apt-get install python-mysql
import Mysqldb
conn=connect(参数列表)
cursor1=conn.cursor()
#encoding=utf-8 import MySQLdb try: conn=MySQLdb.connect(host=‘localhost‘,port=3306,db=‘test1‘,user=‘root‘,passwd=‘mysql‘,charset=‘utf8‘) cs1=conn.cursor() count=cs1.execute("insert into students(sname) values(‘张良‘)") print count conn.commit() cs1.close() conn.close() except Exception,e: print e.message
#encoding=utf-8 import MySQLdb try: conn=MySQLdb.connect(host=‘localhost‘,port=3306,db=‘test1‘,user=‘root‘,passwd=‘mysql‘,charset=‘utf8‘) cs1=conn.cursor() count=cs1.execute("update students set sname=‘刘邦‘ where id=6") print count conn.commit() cs1.close() conn.close() except Exception,e: print e.message
#encoding=utf-8 import MySQLdb try: conn=MySQLdb.connect(host=‘localhost‘,port=3306,db=‘test1‘,user=‘root‘,passwd=‘mysql‘,charset=‘utf8‘) cs1=conn.cursor() count=cs1.execute("delete from students where id=6") print count conn.commit() cs1.close() conn.close() except Exception,e: print e.message
#encoding=utf-8 import MySQLdb try: conn=MySQLdb.connect(host=‘localhost‘,port=3306,db=‘test1‘,user=‘root‘,passwd=‘mysql‘,charset=‘utf8‘) cs1=conn.cursor() sname=raw_input("请输入学生姓名:") params=[sname] count=cs1.execute(‘insert into students(sname) values(%s)‘,params) print count conn.commit() cs1.close() conn.close() except Exception,e: print e.message
#encoding=utf8 import MySQLdb try: conn=MySQLdb.connect(host=‘localhost‘,port=3306,db=‘test1‘,user=‘root‘,passwd=‘mysql‘,charset=‘utf8‘) cur=conn.cursor() cur.execute(‘select * from students where id=7‘) result=cur.fetchone() print result cur.close() conn.close() except Exception,e: print e.message
#encoding=utf8 import MySQLdb try: conn=MySQLdb.connect(host=‘localhost‘,port=3306,db=‘test1‘,user=‘root‘,passwd=‘mysql‘,charset=‘utf8‘) cur=conn.cursor() cur.execute(‘select * from students‘) result=cur.fetchall() print result cur.close() conn.close() except Exception,e: print e.message
原文:https://www.cnblogs.com/leecoffee/p/9039001.html