python pymysql-增删改查

pymysql-增删改查

1. pymysql-增删改查

#!/usr/bin/env python3
# _*_ coding: utf-8 _*_
# Author:shichao
# File: .py

import pymysql
conn = pymysql.connect(host='127.0.0.1',
                       port=3306,
                       user='test',
                       password='Test@963852',
                       db='test',
                       charset='utf8',
                       cursorclass=pymysql.cursors.DictCursor)
try:
    with conn.cursor() as cursor:
        sql = "select id,username,password from user"
        cursor.execute(sql)
        result = cursor.fetchall()    # 获取所有数据
        for dict in result:
            print(f"ID: {dict['id']}, 用户名: {dict['username']}, 密码: {dict['password']}")

    # 修改数据
    with conn.cursor() as cursor:                                 # 游标
        sql = "update user set username='test2' where  id= 3"
        cursor.execute(sql)
        conn.commit()
finally:
    conn.close()     # mysql关闭
posted @   七月流星雨  阅读(149)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示