1 Star 0 Fork 19

小春/Python_Learning

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
select.py 589 Bytes
一键复制 编辑 原始数据 按行查看 历史
阿基米东 提交于 2020-09-10 20:44 +08:00 . pymysql
import pymysql
# 打开数据库连接
db = pymysql.connect(host="localhost",user="root",password="",db="test",charset="utf8")
# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()
# 定义查询 sql 语句
sql = "SELECT * FROM USER"
try:
# 使用 execute() 方法执行 SQL 查询
cursor.execute(sql)
print("本次查询条数:", cursor.rowcount)
# 获取所有结果
res = cursor.fetchall()
for item in res:
print(item)
except Exception as err:
print("SQL执行错误,原因:",err)
# 关闭数据库连接
db.close()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/wython/Python_Learning.git
git@gitee.com:wython/Python_Learning.git
wython
Python_Learning
Python_Learning
master

搜索帮助