# 读取数据库
# 链接数据库 # 创建一个查询页 # 输入sql语句 # 查询 # 查看结果 # 关闭查询页 # 关闭数据库
import pymysql class ReadMysql: def __init__(self): self.mysql = pymysql.connect(host=‘‘, user=‘‘, password=‘‘, charset=‘utf8‘, port=3306) self.cursor = self.mysql.cursor() def fetch_one(self, sql): self.cursor.execute(sql) result = self.cursor.fetchone()[0] print(result) def fetch_all(self, sql): self.cursor.execute(sql) result = self.cursor.fetchall()[0][0] print(result) def close(self): self.cursor.close() self.mysql.close() if __name__ == ‘__main__‘:
原文地址:https://www.cnblogs.com/sophia-985935365/p/12634467.html
时间: 2024-10-10 14:33:10