zoukankan      html  css  js  c++  java
  • Mysql:PDBC(Python操作数据库-mysql)

    资料

    https://pymysql.readthedocs.io/en/latest/index.html

    安装

    pip install pymysql

    ⚠️:MySQL Server – one of the following:

    #!/usr/bin/env/python3
    # -*- coding:utf-8 -*-
    """
    @project: demo
    @author: zy7y
    @file: pymysql_demo.py
    @ide: PyCharm
    @time: 2020/8/26
    """
    
    import pymysql
    
    # 连接到数据库:创建 操作数据库对象
    connection = pymysql.connect(host='192.168.0.222',
                                 user='root',
                                 password='root',
                                 db='school',
                                 charset='utf8',
                                 # pymysql.cursors.DictCursor 以字典形式返回游标
                                 cursorclass=pymysql.cursors.DictCursor)
    sql = "select * from student"
    # 创建 执行sql对象
    cursor = connection.cursor()
    # 执行sql
    cursor.execute(sql)
    # 增加、删除、修改 需要使用到 cursor.commit() 来提交
    # fetchall() 返回所有结果 , fetchone() 返回第一个结果, fetchmany(返回结果条数)
    result = cursor.fetchall()
    print(result)
    # 关闭数据库对象
    connection.close()
    

    官方推荐(部分已被我修改,完整的可查看上方资料)

    #!/usr/bin/env/python3
    # -*- coding:utf-8 -*-
    """
    @project: demo
    @author: zy7y
    @file: pymysql_demo.py
    @ide: PyCharm
    @time: 2020/8/26
    """
    
    import pymysql
    
    # 连接到数据库:创建 操作数据库对象
    connection = pymysql.connect(host='192.168.0.222',
                                 user='root',
                                 password='root',
                                 db='school',
                                 charset='utf8',
                                 # pymysql.cursors.DictCursor 以字典形式返回游标
                                 cursorclass=pymysql.cursors.DictCursor)
    
    # 官网推荐写法:使用 try ... finally,以及with上下文管理
    try:
        # 查询
        with connection.cursor() as cursor:
            sql = "select * from student"
            cursor.execute(sql)
            result = cursor.fetchone()
            print(result)
    
        # 修改
        with connection.cursor() as cursor:
            sql = "update student set loginpwd=%s where studentno = 1000"
            # 测试回滚
            zero = 0/0
            # 此处root 将被替换进sql字符串中第一个%s,最终sql等于:update student set loginpwd='root' where studentno = 1000
            cursor.execute(sql, ('root12345611',))
            # 增加、删除、修改 需要提交, 这里不会自动提交事务
            connection.commit()
            # 查看改变行数,如果没有改变则返回0, 改变则返回行数
            row = cursor.rowcount
            if row:
                print('修改成功')
            else:
                print('没有任何修改.')
    except Exception as e:
        # 发生错误 回滚
        connection.rollback()
        print(f'发生错误{e},已执行回滚')
    finally:
        # 关闭游标对象
        cursor.close()
        # 最后关闭数据库连接
        connection.close()
    
    作者:zy7y
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文链接,否则保留追究法律责任的权利。
  • 相关阅读:
    SQL Server中的Merge关键字
    详解公用表表达式(CTE)
    SQL Server优化50法
    Chrome下的脚本管理器
    初步设计了一下视频工具合集的界面
    迅雷的剪贴板冲突好强大
    在C#中用MediaInfo获取视频或音频的属性
    用Command模式简单的实现Undo&Redo功能
    用DoddleReport快速生成报表
    移动支付时代早日来临吧
  • 原文地址:https://www.cnblogs.com/zy7y/p/13568539.html
Copyright © 2011-2022 走看看