码迷,mamicode.com
首页 > 数据库 > 详细

python模块----pymysql模块 (连接MySQL数据库)

时间:2020-05-06 21:38:33      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:使用   etc   install   数据   记录   标准   man   from   python   

pymysql模块是专门用来连接mysql数据库的模块,是非标准库模块,需要pip下载

下载

pip  install pymysql
  1. 查询
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 e
finally:
    db.close()  # 关闭连接
print(‘获取剩余结果的第一行数据{}‘.format(cursor.fetchone())) 
print(‘获取剩余结果的前N行数据{}‘.format(cursor.fetchmany(2)))
print(‘获取剩余结果的全部数据{}‘.format(cursor.fetchall()))

python模块----pymysql模块 (连接MySQL数据库)

标签:使用   etc   install   数据   记录   标准   man   from   python   

原文地址:https://www.cnblogs.com/du-z/p/12838766.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!