PyMySql -- 常用方法

1、连接数据库:

以下实例链接 Mysql 的 TESTDB 数据库:

import pymysql
 
# 打开数据库连接
 db = pymysql.connect(host='localhost', port=3306, user='root',
              password='xxxx', db='books', charset='utf8')
# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()
 
# 使用 execute()  方法执行 SQL 查询 
cursor.execute("SELECT VERSION()")
 
# 使用 fetchone() 方法获取单条数据.
data = cursor.fetchone()
 
print ("Database version : %s " % data)
 
# 关闭数据库连接
db.close()

 2、数据库查询操作

Python查询Mysql使用 fetchone() 方法获取单条数据, 使用fetchall() 方法获取多条数据。

  • fetchone(): 该方法获取下一个查询结果集。结果集是一个对象
  • fetchall(): 接收全部的返回结果行.
  • rowcount: 这是一个只读属性,并返回执行execute()方法后影响的行数。
原文地址:https://www.cnblogs.com/gengyufei/p/12690830.html