One - One Code All

Blog Content

pymysql的使用

Python MySQL   2012-10-10 21:25:31

pymysql的google用户组:https://groups.google.com/forum/#!forum/pymysql-users


pymysql用法:

import pymysql
 
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='db')
cursor = conn.cursor()
cursor.execute("select * from table")
 
# 获取剩余结果的第一行数据
row = cursor.fetchone()

# 获取剩余结果前n行数据
row = cursor.fetchmany(3)
 
# 获取剩余结果所有数据
row = cursor.fetchall()


# 更新
# 执行SQL,并返回受影响行数
row = cursor.execute("update table set title = '123' where id = %s", (1,))
conn.commit()
 
# 插入
row = cursor.executemany("insert into table(title)values(%s)", [("11111"),("22222")])
conn.commit()


#获取自增id
new_id = cursor.lastrowid      

cursor.close()
conn.close()



上一篇:PHP-MySQL:Headers and client library minor version mismatch
下一篇:python操作mysql,pymysql操作方法

The minute you think of giving up, think of the reason why you held on so long.