美文网首页
PyMySQL库安装

PyMySQL库安装

作者: wangyu2488 | 来源:发表于2019-12-22 13:31 被阅读0次

PyMySQL遵循Python DB-API2规范

2019年12月10日

https://pypi.org/project/PyMySQL/

安装

python3 -m pip install PyMySQL (多试几次 有时候网络问题会报错)


image.png

一般用法

import pymysql
# 1. 建立数据库连接
connection = pymysql.connect(host='localhost',
                             user='root',
                             password='wy123456',
                             database='testdb',
                             charset='utf8')
try:
    # 2. 创建游标对象
    with connection.cursor() as cursor:
        # 3. 执行SQL操作
        # sql = 'select name, userid from user where userid >%s'
        # cursor.execute(sql, [0])
        sql = 'select name, userid from user where userid >%(id)s'
        cursor.execute(sql, {'id': 0})
        # 4. 提取结果集
        result_set = cursor.fetchall()
        for row in result_set:
            print('id:{0} - name:{1}'.format(row[1], row[0]))
    # with代码块结束 5. 关闭游标
finally:
    # 6. 关闭数据连接
    connection.close()

如果您发现本文对你有所帮助,如果您认为其他人也可能受益,请把它分享出去。

相关文章

网友评论

      本文标题:PyMySQL库安装

      本文链接:https://www.haomeiwen.com/subject/ufjonctx.html