• 数据库连接和数据库池


     

    Pymysql

    1. 安装  
    2. pip install PyMySQL
    3. # -*- coding: utf-8 -*-  
    4. import pymysql  
    5. conn = pymysql.connect(host="192.168.32.71"user="root",password="redhat",database="my_sql",charset="utf8")  
    6. cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)  
    7.     
    8. sql = "select * from name"  
    9. res = cursor.execute(sql)  
    10. print(cursor.fetchall())  
    11.     
    12. # [{'name': 'peach', 'id': 1, 'age': 25}, {'name': 'taoiz', 'id': 2, 'age': 23}]  
    13. cursor.close()  
    14. conn.close() 

     

    数据库连接池DBUtils

    1. 安装  
    2. pip install DBUtils -i https://pypi.douban.com/simple  
    3.     
    4.     
    5. 数据库连接池  
    6. # -*- coding: utf-8 -*-  
    7. import pymysql  
    8. from DBUtils.PooledDB import PooledDB  
    9. POOL = PooledDB(  
    10.     creator=pymysql,  # 使用链接数据库的模块  
    11.     maxconnections=6, # 连接池允许的最大连接数,0None表示不限制连接数  
    12.     mincached=2,      # 初始化时,链接池中至少创建的空闲的链接,0表示不创建  
    13.     maxcached=5,      # 链接池中最多闲置的链接,0None不限制  
    14.     maxshared=3,      # 链接池中最多共享的链接数量,0None表示全部共享。PS: 无用,因为pymysqlMySQLdb等模块的 threadsafety都为1,所有值无论设置为多少,_maxcached永远为0,所以永远是所有链接都共享。  
    15.     
    16.     blocking=True,    # 连接池中如果没有可用连接后,是否阻塞等待。True,等待;False,不等待然后报错  
    17.     maxusage=None,    # 一个链接最多被重复使用的次数,None表示无限制  
    18.     setsession=[],    # 开始会话前执行的命令列表。如:["set datestyle to ...", "set time zone ..."] 比如设置数据库的开始时间 set firstday=3  
    19.     
    20.     ping=0,  
    21.                        # ping MySQL服务端,检查是否服务可用。  
    22.                        #  如:0 = None = never,  
    23.                        # 1 = default = whenever it is requested,  
    24.                        # 2 = when a cursor is created,  
    25.                        # 4 = when a query is executed,  
    26.                        # 7 = always  
    27.     host="192.168.32.71",  
    28.     port=3306,  
    29.     user="root",  
    30.     password="redhat",  
    31.     charset="utf8",  
    32.     db="my_sql"  
    33. )  
    34.     
    35. 使用  
    36. conn = POOL.connection()  # pymysql - conn  
    37. cur = conn.cursor(cursor=pymysql.cursors.DictCursor)  
    38.     
    39. res = cur.execute("select * from name")  
    40. print(cur.fetchall())  
    41. # [{'name': 'peach', 'id': 1, 'age': 25}, {'name': 'taoiz', 'id': 2, 'age': 23}]  
    42.     
    43. 关闭  
    44. cur.close()  
    45. conn.close()

    自己封装的sqlhelper

    1. from dbpool import POOL  
    2. import pymysql  
    3.     
    4. def create_conn():  
    5.     conn = POOL.connection()  
    6.     cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)  
    7.     return conn,cursor  
    8.     
    9. def close_conn(conn,cursor):  
    10.     cursor.close()  
    11.     conn.close()  
    12.     
    13. def insert(sql,args):  
    14.     conn,cursor = create_conn()  
    15.     res = cursor.execute(sql,args)  
    16.     conn.commit()  
    17.     close_conn(conn,cursor)  
    18.     return res  
    19.     
    20. def fetch_one(sql,args):  
    21.     conn,cursor = create_conn()  
    22.     cursor.execute(sql,args)  
    23.     res = cursor.fetchone()  
    24.     close_conn(conn,cursor)  
    25.     return res  
    26.     
    27. def fetch_all(sql,args):  
    28.     conn,cursor = create_conn()  
    29.     cursor.execute(sql,args)  
    30.     res = cursor.fetchall()  
    31.     close_conn(conn,cursor)  
    32.     return res  
    33.     
    34. sql = "insert into users(name,age) VALUES (%s, %s)"  
    35.     
    36. # insert(sql,("mjj",9))  
    37.     
    38. sql = "select * from users where name=%s and age=%s"  
    39.     
    40. print(fetch_one(sql,("mjj",9)))  
  • 相关阅读:
    杂文: 日剧《轮到你了》第7集中的组合数学问题
    CF 板刷总结
    【题解】P2324 [SCOI2005]骑士精神
    【模板】 $ ext{K}$ 短路
    P4554 小明的游戏
    [题解]P1856 [USACO5.5]矩形周长Picture
    洛谷P2243 电路维修
    【题解】洛谷P3660 [USACO17FEB]Why Did the Cow Cross the Road III
    【题解】P1119 灾后重建
    集福卡活动
  • 原文地址:https://www.cnblogs.com/py-web/p/12068108.html
Copyright © 2020-2023  润新知