数据连接_数据连接电脑

数据连接_数据连接电脑Pymysql 安装 pipinstallPyMySQL #-*-coding:utf-8-*- importpymysql conn=pymysql.connect(host="192.1

 

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)))  

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
转载请注明出处: https://daima100.com/10416.html

(0)
上一篇 2022-12-28 08:30
下一篇 2022-12-28

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注