Python知识分享网 - 专业的Python学习网站 学Python,上Python222
pymysql执行select查询操作
发布于:2023-09-19 10:43:56

1小时学会 Python操作Mysql数据库之pymysql模块技术https://www.bilibili.com/video/BV1Dz4y1j7Jr

 

 

 

pymysql执行DML语句

MySQL 数据库模块同样可以使用游标的execute()方法执行DML(Data Manipulation Language, 数据操纵语言)的 insert、update、delete语句,对数据库进行插入、修改和删除数据操作。

 

pymysql执行select查询操作

 

con = None

try:
    # 创建数据库连接
    con = Connection(
        host="localhost",  # 主机名
        port=3306,  # 端口
        user="root",  # 账户
        password="123456",  # 密码
        database="db_python"  # 指定操作的数据库
    )
    # 获取游标对象
    cursor = con.cursor()
    # 使用游标对象,执行sql语句
    cursor.execute("select * from t_student")
    # 获取查询所有结果
    result = cursor.fetchall()
    print(type(result), result)
    for row in result:
        print(row)
except Exception as e:
    print("异常:", e)
finally:
    if con:
        # 关闭连接
        con.close()

 

 

 

转载自: