原文:http://www.the5fire.com/python-opt-mysql.html
1、增:
import MySQLdb conn = MySQLdb.connect(db='test',host='127.0.0.1',user='root',passwd='root') curs = conn.cursor() sql_insert = 'INSERT INTO books(title) VALUES("book2")' curs.execute(sql_insert) conn.commit()
2、删:
sql_delete = 'DELETE FROM books WHERE id = 1' curs.execute(sql_delete)
3、改:
sql_update = 'UPDATE books SET title = "bookupdate" WHERE id = 1' curs.execute(sql_update)
4、查:
sql_select = 'SELECT * FROM books' curs.execute(sql_select) rows = curs.fetchall() for row in rows: print row[1]
还有另一个更详细的使用py操作Mysql的经验心得文章:
python下的MySQLdb使用
另一个比较详细的
官方给出的mysql python 的在线帮助文档
如:MySQLCursor查询返回k,v结构的方法