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关闭
标签:python,改查,pymysql,cursor,dict,sql,conn
From: https://www.cnblogs.com/scajy/p/17048778.html