不使用连接池:
import pymysql
mysql_config = {
"db": "test_db",
"host": "127.0.0.1",
"user": "user",
"password": "password",
}
conn = pymysql.connect(**mysql_config)
cursor = conn.cursor()
cursor.execute("""select * from my_table""")
all_result = cursor.fetchall() # 得到所有结果
cursor.close()
conn.close()
使用连接池:
from DBUtils.PooledDB import PooledDB
mysql_config = {
"db": "test_db",
"host": "127.0.0.1",
"user": "user",
"password": "password",
}
pool = PooledDB(pymysql, 5, **mysql_config)
conn = pool.connection()
cursor = conn.cursor()
cursor.execute("""select * from my_table""")
all_result = cursor.fetchall() # 得到所有结果
cursor.close()
conn.close()