python模块----pymysql模块 (连接MySQL数据库),,pymysql模块是
python模块----pymysql模块 (连接MySQL数据库),,pymysql模块是
pymysql模块是专门用来连接mysql数据库的模块,是非标准库模块,需要pip下载
下载
pip install pymysql查询
import pymysql# 打开数据库连接db = pymysql.connect(host="192.168.254.24", user="root",password="root", db="mysql", port=3306)# 使用cursor()方法获取操作游标cur = db.cursor()# 编写sql 查询语句 user 对应我的表名sql = "select host,user,password from user"try: cur.execute(sql) # 执行sql语句 results = cur.fetchall() # 获取查询的所有记录 for i in results:#遍历结果 print(i)except Exception as e: raise efinally: db.close() # 关闭连接
print(‘获取剩余结果的第一行数据{}‘.format(cursor.fetchone())) print(‘获取剩余结果的前N行数据{}‘.format(cursor.fetchmany(2)))print(‘获取剩余结果的全部数据{}‘.format(cursor.fetchall()))
python模块----pymysql模块 (连接MySQL数据库)
评论关闭