import pymysql #todo 单点连接数据库 pd = pymysql.connect(host=‘127.0.0.1‘,port=3306,user=‘root‘,passwd=‘123456‘,db=‘test‘,charset=‘utf8‘) cursor = pd.cursor() sql = ‘select * from class‘ cursor.execute(sql) class_data = cursor.fetchall() print(class_data) cursor.close() pd.cursor()
import pymysql #todo 数据库连接池连接数据库 from DBUtils.PooledDB import PooledDB pool_db = PooledDB(pymysql,5,host=‘127.0.0.1‘,port=3306,user=‘root‘,passwd=‘123456‘,db=‘test‘,charset=‘utf8‘) #5为连接池里的最少连接数 coon = pool_db.connection() #以后每次需要数据库连接就是用connection()函数获取连接就好了 cursor = coon.cursor() sql = ‘select * from class‘ cursor.execute(sql) class_data = cursor.fetchall() print(class_data)