# db_libs
## 安装 pip innstall db_libs
各种数据库的封装。只封装生成连接的部分,十分克制,很少去添加一些新的方法然后去调用原生类的方法。
因为原生类已经很好用了,主要是控制一下用户无限实例化连接类,造成反复创建连接就可以了。
```
redis_lib和mongo_lib在redis和pymongo的基础上进行封装。
由于原生的包已经足够好用了,并不需要重新写几百个方法进行过度封装。
仅仅是加了享元模式,使得在相同入参情况下无限实例化相关客户端类的时候不会反复重新连接。
封装方式采用的非常规方式,使用的是继承方式,而非通常情况下的使用组合来进行封装。
继承方式封装的比组合模式封装的好处更多。
mysql_lib使用连接池,兼容在多线程环境运行。使调用时候少关注cursor commit close 等。
sqla_lib 是反射已存在的表,也是使用连接池,兼容多线程环境运行。能够支持orm和原生sql语句两种执行方式。
```
```python
# 组合模式封装的代码一般是如下这种例子。
"""
这种方式封装是组合,精确点是23种设计模式的代理模式。
代理模式说的是定义很多方法,来调用self.r所具有的方法。
另外对于无限实例化,还使用了享元模式。
封装数据库切记不要使用单例模式,如果入参传了不同的主机ip或者不同的db,而仍然返回之前的连接对象,那就大错特错了。
"""
# 使用组合模式封装的redis,没有继承模式的好用。
import redis
class RedisClient:
params__reids_map = {}
def __init__(self,host,db,port,password):
if (host,db,port,password) not in self.__class__.params__reids_map:
self.r = redis.Redis(host,db,port,password)
self.__class__.params__reids_map[(host,db,port,password)] = self.r
else:
self.r = self.__class__.params__reids_map[(host,db,port,password)]
def my_set(self,key,value):
print('额外的扩展')
self.r.set(name=key,value=value)
def my_get(self,name):
# 这个封装简直是多次一举,在redis实例化时候,
# 将Redis类的构造方法的入参 decode_responses设置为True,就可以避免几百个方法需要反复decode了。
return self.r.get(name).decode()
```
#### 例如网上的一种封装,重新封装几百个方法,我不喜欢这样封装工具类。
```python
import redis
class MyRedis():
def __init__(self,ip,password,port=6379,db=1):#构造函数
try:
self.r = redis.Redis(host=ip,password=password,port=port,db=db) #连接redis固定方法,这里的值必须固定写死
except Exception as e:
print('redis连接失败,错误信息%s'%e)
def str_get(self,k):
res = self.r.get(k) #会从服务器传对应的值过来,性能慢
if res:
return res.decode() #从redis里面拿到的是bytes类型的数据,需要转换一下
def str_set(self,k,v,time=None): #time默认失效时间
self.r.set(k,v,time)
def delete(self,k):
tag = self.r.exists(k)
#判断这个key是否存在,相对于get到这个key他只是传回一个存在火灾不存在的信息,
# 而不用将整个k值传过来(如果k里面存的东西比较多,那么传输很耗时)
if tag:
self.r.delete(k)
else:
print('这个key不存在')
def hash_get(self,name,k): #哈希类型存储的是多层字典(嵌套字典)
res = self.r.hget(name,k)
if res:
return res.decode() #因为get不到值得话也不会报错所以需要判断一下
def hash_set(self,name,k,v): #哈希类型的是多层
self.r.hset(name,k,v) #set也不会报错
def hash_getall(self,name):
res = self.r.hgetall(name) #得到的是字典类型的,里面的k,v都是bytes类型的
data={}
if res:
for k,v in res.items(): #循环取出字典里面的k,v,在进行decode
k = k.decode()
v = v.decode()
data[k]=v
return data
def hash_del(self,name,k):
res = self.r.hdel(name,k)
if res:
print('删除成功')
return 1
else:
print('删除失败,该key不存在')
return 0
@property #属性方法,
# 使用的时候和变量一个用法就好比实例,A=MyRedis(), A.clean_redis使用,
# 如果不加这个@property,使用时A=MyRedis(), A.clean_redis() 后面需要加这个函数的括号
def clean_redis(self):
self.r.flushdb() #清空 redis
print('清空redis成功!')
return 0
a = MyRedis('118.0000','HK0000*')
print(a.str_get('duan'))
```
## sqla_lib使用如下,可以orm操作已存在的数据库。
```python
if __name__ == '__main__':
"""
例如 ihome_area2的表结果如下。
create table ihome_area2
(
create_time datetime null,
update_time datetime null,
id int auto_increment
primary key,
name varchar(32) not null
);
"""
enginex = create_engine(
'mysql+pymysql://root:123456@127.0.0.1:3306/aj?charset=utf8',
max_overflow=10, # 超过连接池大小外最多创建的连接
pool_size=50, # 连接池大小
pool_timeout=30, # 池中没有线程最多等待的时间,否则报错
pool_recycle=3600, # 多久之后对线程池中的线程进行一次连接的回收(重置)
echo=True)
sqla_helper = SqlaReflectHelper(enginex)
Ihome_area2 = sqla_helper.base_classes.ihome_area2 # ihome_area2是表名。
def f1():
with sqla_helper.session as ss:
ss # type: _SessionContext
print(ss)
print(ss.query(sqlalchemy.func.count(Ihome_area2.id)).scalar())
# 使用orm方式插入
ss.add(Ihome_area2(create_time=datetime.now(), update_time=datetime.now(), name='testname'))
print(ss.query(sqlalchemy.func.count(Ihome_area2.id)).scalar())
# 使用占位符语法插入,此种可以防止sql注入
ss.execute(f'''INSERT INTO ihome_area2 (create_time, update_time, name) VALUES (:v1,:v2,:v3)''', params={'v1': '2020-06-14 19:15:14', 'v2': '2020-06-14 19:15:14', 'v3': 'testname00'})
# 直接自己拼接完整字符串,不使用三方包占位符的后面的参数,此种会引起sql注入,不推荐。
cur = ss.execute(f'''INSERT INTO ihome_area2 (create_time, update_time, name) VALUES ('2020-06-14 19:15:14','2020-06-14 19:15:14', 'testname')''', )
# 这样也可以打印执行的语句
# noinspection PyProtectedMember
print(cur._saved_cursor._executed)
# 使用最原生的语句,直接调用了pymysql的cursor对象。
conny = sqla_helper.engine.raw_connection()
cury = conny.cursor(DictCursor) # type: DictCursor
print(cury)
cury.execute('SELECT * FROM ihome_area2 LIMIT 3')
result = cury.fetchall()
print(result)
conny.commit()
cury.close()
conny.close()
def f2():
ss = sqla_helper.get_session_factory()()
print(ss)
print(ss.query(sqlalchemy.func.count(sqla_helper.base_classes.ihome_area.id)).scalar())
ss.add(sqla_helper.base_classes.ihome_area(create_time=datetime.now(), update_time=datetime.now(), name='testname'))
ss.commit()
print(ss.query(sqlalchemy.func.count(sqla_helper.base_classes.ihome_area.id)).scalar())
ss.close()
with decorator_libs.TimerContextManager():
t_pool = BoundedThreadPoolExecutor(10) # 封装mysql,切记一定要测试多线程下的情况。
for _ in range(500):
# f1()
t_pool.submit(f1)
t_pool.shutdown()
```
![](https://visitor-badge.glitch.me/badge?page_id=db_libs)
Raw data
{
"_id": null,
"home_page": "",
"name": "db-libs",
"maintainer": "ydf",
"docs_url": null,
"requires_python": "",
"maintainer_email": "ydf0509@sohu.com",
"keywords": "database,redis,mongo,elasticsearch,mysql,sqlachemy",
"author": "bfzs",
"author_email": "ydf0509@sohu.com",
"download_url": "https://files.pythonhosted.org/packages/3a/7c/0a73b99ceb16a69b6b79a6be209fc5dbab614aa7dbb787443398b9175719/db_libs-1.0.tar.gz",
"platform": "all",
"description": "# db_libs\r\n## \u5b89\u88c5 pip innstall db_libs\r\n\r\n\u5404\u79cd\u6570\u636e\u5e93\u7684\u5c01\u88c5\u3002\u53ea\u5c01\u88c5\u751f\u6210\u8fde\u63a5\u7684\u90e8\u5206\uff0c\u5341\u5206\u514b\u5236,\u5f88\u5c11\u53bb\u6dfb\u52a0\u4e00\u4e9b\u65b0\u7684\u65b9\u6cd5\u7136\u540e\u53bb\u8c03\u7528\u539f\u751f\u7c7b\u7684\u65b9\u6cd5\u3002\r\n\r\n\u56e0\u4e3a\u539f\u751f\u7c7b\u5df2\u7ecf\u5f88\u597d\u7528\u4e86\uff0c\u4e3b\u8981\u662f\u63a7\u5236\u4e00\u4e0b\u7528\u6237\u65e0\u9650\u5b9e\u4f8b\u5316\u8fde\u63a5\u7c7b\uff0c\u9020\u6210\u53cd\u590d\u521b\u5efa\u8fde\u63a5\u5c31\u53ef\u4ee5\u4e86\u3002\r\n\r\n```\r\nredis_lib\u548cmongo_lib\u5728redis\u548cpymongo\u7684\u57fa\u7840\u4e0a\u8fdb\u884c\u5c01\u88c5\u3002\r\n\u7531\u4e8e\u539f\u751f\u7684\u5305\u5df2\u7ecf\u8db3\u591f\u597d\u7528\u4e86\uff0c\u5e76\u4e0d\u9700\u8981\u91cd\u65b0\u5199\u51e0\u767e\u4e2a\u65b9\u6cd5\u8fdb\u884c\u8fc7\u5ea6\u5c01\u88c5\u3002\r\n\u4ec5\u4ec5\u662f\u52a0\u4e86\u4eab\u5143\u6a21\u5f0f\uff0c\u4f7f\u5f97\u5728\u76f8\u540c\u5165\u53c2\u60c5\u51b5\u4e0b\u65e0\u9650\u5b9e\u4f8b\u5316\u76f8\u5173\u5ba2\u6237\u7aef\u7c7b\u7684\u65f6\u5019\u4e0d\u4f1a\u53cd\u590d\u91cd\u65b0\u8fde\u63a5\u3002\r\n\u5c01\u88c5\u65b9\u5f0f\u91c7\u7528\u7684\u975e\u5e38\u89c4\u65b9\u5f0f\uff0c\u4f7f\u7528\u7684\u662f\u7ee7\u627f\u65b9\u5f0f\uff0c\u800c\u975e\u901a\u5e38\u60c5\u51b5\u4e0b\u7684\u4f7f\u7528\u7ec4\u5408\u6765\u8fdb\u884c\u5c01\u88c5\u3002\r\n\u7ee7\u627f\u65b9\u5f0f\u5c01\u88c5\u7684\u6bd4\u7ec4\u5408\u6a21\u5f0f\u5c01\u88c5\u7684\u597d\u5904\u66f4\u591a\u3002\r\n\r\n\r\n\r\nmysql_lib\u4f7f\u7528\u8fde\u63a5\u6c60\uff0c\u517c\u5bb9\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u8fd0\u884c\u3002\u4f7f\u8c03\u7528\u65f6\u5019\u5c11\u5173\u6ce8cursor commit close \u7b49\u3002\r\n\r\nsqla_lib \u662f\u53cd\u5c04\u5df2\u5b58\u5728\u7684\u8868\uff0c\u4e5f\u662f\u4f7f\u7528\u8fde\u63a5\u6c60\uff0c\u517c\u5bb9\u591a\u7ebf\u7a0b\u73af\u5883\u8fd0\u884c\u3002\u80fd\u591f\u652f\u6301orm\u548c\u539f\u751fsql\u8bed\u53e5\u4e24\u79cd\u6267\u884c\u65b9\u5f0f\u3002\r\n\r\n```\r\n\r\n```python\r\n# \u7ec4\u5408\u6a21\u5f0f\u5c01\u88c5\u7684\u4ee3\u7801\u4e00\u822c\u662f\u5982\u4e0b\u8fd9\u79cd\u4f8b\u5b50\u3002\r\n\"\"\"\r\n\u8fd9\u79cd\u65b9\u5f0f\u5c01\u88c5\u662f\u7ec4\u5408\uff0c\u7cbe\u786e\u70b9\u662f23\u79cd\u8bbe\u8ba1\u6a21\u5f0f\u7684\u4ee3\u7406\u6a21\u5f0f\u3002\r\n\u4ee3\u7406\u6a21\u5f0f\u8bf4\u7684\u662f\u5b9a\u4e49\u5f88\u591a\u65b9\u6cd5\uff0c\u6765\u8c03\u7528self.r\u6240\u5177\u6709\u7684\u65b9\u6cd5\u3002\r\n\r\n\r\n\u53e6\u5916\u5bf9\u4e8e\u65e0\u9650\u5b9e\u4f8b\u5316\uff0c\u8fd8\u4f7f\u7528\u4e86\u4eab\u5143\u6a21\u5f0f\u3002\r\n\u5c01\u88c5\u6570\u636e\u5e93\u5207\u8bb0\u4e0d\u8981\u4f7f\u7528\u5355\u4f8b\u6a21\u5f0f\uff0c\u5982\u679c\u5165\u53c2\u4f20\u4e86\u4e0d\u540c\u7684\u4e3b\u673aip\u6216\u8005\u4e0d\u540c\u7684db\uff0c\u800c\u4ecd\u7136\u8fd4\u56de\u4e4b\u524d\u7684\u8fde\u63a5\u5bf9\u8c61\uff0c\u90a3\u5c31\u5927\u9519\u7279\u9519\u4e86\u3002\r\n\r\n\"\"\"\r\n# \u4f7f\u7528\u7ec4\u5408\u6a21\u5f0f\u5c01\u88c5\u7684redis\uff0c\u6ca1\u6709\u7ee7\u627f\u6a21\u5f0f\u7684\u597d\u7528\u3002\r\nimport redis\r\n\r\nclass RedisClient:\r\n\r\n params__reids_map = {}\r\n\r\n def __init__(self,host,db,port,password):\r\n if (host,db,port,password) not in self.__class__.params__reids_map:\r\n self.r = redis.Redis(host,db,port,password)\r\n self.__class__.params__reids_map[(host,db,port,password)] = self.r\r\n else:\r\n self.r = self.__class__.params__reids_map[(host,db,port,password)]\r\n \r\n def my_set(self,key,value):\r\n print('\u989d\u5916\u7684\u6269\u5c55')\r\n self.r.set(name=key,value=value)\r\n \r\n def my_get(self,name):\r\n # \u8fd9\u4e2a\u5c01\u88c5\u7b80\u76f4\u662f\u591a\u6b21\u4e00\u4e3e\uff0c\u5728redis\u5b9e\u4f8b\u5316\u65f6\u5019\uff0c\r\n # \u5c06Redis\u7c7b\u7684\u6784\u9020\u65b9\u6cd5\u7684\u5165\u53c2 decode_responses\u8bbe\u7f6e\u4e3aTrue\uff0c\u5c31\u53ef\u4ee5\u907f\u514d\u51e0\u767e\u4e2a\u65b9\u6cd5\u9700\u8981\u53cd\u590ddecode\u4e86\u3002\r\n return self.r.get(name).decode()\r\n\r\n```\r\n\r\n#### \u4f8b\u5982\u7f51\u4e0a\u7684\u4e00\u79cd\u5c01\u88c5\uff0c\u91cd\u65b0\u5c01\u88c5\u51e0\u767e\u4e2a\u65b9\u6cd5\uff0c\u6211\u4e0d\u559c\u6b22\u8fd9\u6837\u5c01\u88c5\u5de5\u5177\u7c7b\u3002\r\n```python\r\nimport redis\r\n\r\nclass MyRedis():\r\n def __init__(self,ip,password,port=6379,db=1):#\u6784\u9020\u51fd\u6570\r\n try:\r\n self.r = redis.Redis(host=ip,password=password,port=port,db=db) #\u8fde\u63a5redis\u56fa\u5b9a\u65b9\u6cd5,\u8fd9\u91cc\u7684\u503c\u5fc5\u987b\u56fa\u5b9a\u5199\u6b7b\r\n except Exception as e:\r\n print('redis\u8fde\u63a5\u5931\u8d25\uff0c\u9519\u8bef\u4fe1\u606f%s'%e)\r\n def str_get(self,k):\r\n res = self.r.get(k) #\u4f1a\u4ece\u670d\u52a1\u5668\u4f20\u5bf9\u5e94\u7684\u503c\u8fc7\u6765\uff0c\u6027\u80fd\u6162\r\n if res:\r\n return res.decode() #\u4eceredis\u91cc\u9762\u62ff\u5230\u7684\u662fbytes\u7c7b\u578b\u7684\u6570\u636e\uff0c\u9700\u8981\u8f6c\u6362\u4e00\u4e0b\r\n\r\n def str_set(self,k,v,time=None): #time\u9ed8\u8ba4\u5931\u6548\u65f6\u95f4\r\n self.r.set(k,v,time)\r\n\r\n def delete(self,k):\r\n tag = self.r.exists(k)\r\n #\u5224\u65ad\u8fd9\u4e2akey\u662f\u5426\u5b58\u5728,\u76f8\u5bf9\u4e8eget\u5230\u8fd9\u4e2akey\u4ed6\u53ea\u662f\u4f20\u56de\u4e00\u4e2a\u5b58\u5728\u706b\u707e\u4e0d\u5b58\u5728\u7684\u4fe1\u606f\uff0c\r\n # \u800c\u4e0d\u7528\u5c06\u6574\u4e2ak\u503c\u4f20\u8fc7\u6765\uff08\u5982\u679ck\u91cc\u9762\u5b58\u7684\u4e1c\u897f\u6bd4\u8f83\u591a\uff0c\u90a3\u4e48\u4f20\u8f93\u5f88\u8017\u65f6\uff09\r\n if tag:\r\n self.r.delete(k)\r\n else:\r\n print('\u8fd9\u4e2akey\u4e0d\u5b58\u5728')\r\n\r\n def hash_get(self,name,k): #\u54c8\u5e0c\u7c7b\u578b\u5b58\u50a8\u7684\u662f\u591a\u5c42\u5b57\u5178\uff08\u5d4c\u5957\u5b57\u5178\uff09\r\n res = self.r.hget(name,k)\r\n if res:\r\n return res.decode() #\u56e0\u4e3aget\u4e0d\u5230\u503c\u5f97\u8bdd\u4e5f\u4e0d\u4f1a\u62a5\u9519\u6240\u4ee5\u9700\u8981\u5224\u65ad\u4e00\u4e0b\r\n\r\n def hash_set(self,name,k,v): #\u54c8\u5e0c\u7c7b\u578b\u7684\u662f\u591a\u5c42\r\n self.r.hset(name,k,v) #set\u4e5f\u4e0d\u4f1a\u62a5\u9519\r\n\r\n def hash_getall(self,name):\r\n res = self.r.hgetall(name) #\u5f97\u5230\u7684\u662f\u5b57\u5178\u7c7b\u578b\u7684\uff0c\u91cc\u9762\u7684k,v\u90fd\u662fbytes\u7c7b\u578b\u7684\r\n data={}\r\n if res:\r\n for k,v in res.items(): #\u5faa\u73af\u53d6\u51fa\u5b57\u5178\u91cc\u9762\u7684k,v\uff0c\u5728\u8fdb\u884cdecode\r\n k = k.decode()\r\n v = v.decode()\r\n data[k]=v\r\n return data\r\n\r\n def hash_del(self,name,k):\r\n res = self.r.hdel(name,k)\r\n if res:\r\n print('\u5220\u9664\u6210\u529f')\r\n return 1\r\n else:\r\n print('\u5220\u9664\u5931\u8d25\uff0c\u8be5key\u4e0d\u5b58\u5728')\r\n return 0\r\n\r\n @property #\u5c5e\u6027\u65b9\u6cd5\uff0c\r\n # \u4f7f\u7528\u7684\u65f6\u5019\u548c\u53d8\u91cf\u4e00\u4e2a\u7528\u6cd5\u5c31\u597d\u6bd4\u5b9e\u4f8b\uff0cA=MyRedis(), A.clean_redis\u4f7f\u7528\uff0c\r\n # \u5982\u679c\u4e0d\u52a0\u8fd9\u4e2a@property,\u4f7f\u7528\u65f6A=MyRedis(), A.clean_redis() \u540e\u9762\u9700\u8981\u52a0\u8fd9\u4e2a\u51fd\u6570\u7684\u62ec\u53f7\r\n def clean_redis(self):\r\n self.r.flushdb() #\u6e05\u7a7a redis\r\n print('\u6e05\u7a7aredis\u6210\u529f\uff01')\r\n return 0\r\n\r\n\r\n\r\na = MyRedis('118.0000','HK0000*')\r\n\r\nprint(a.str_get('duan'))\r\n\r\n```\r\n\r\n\r\n## sqla_lib\u4f7f\u7528\u5982\u4e0b\uff0c\u53ef\u4ee5orm\u64cd\u4f5c\u5df2\u5b58\u5728\u7684\u6570\u636e\u5e93\u3002\r\n```python\r\n\r\nif __name__ == '__main__':\r\n \"\"\"\r\n \u4f8b\u5982 ihome_area2\u7684\u8868\u7ed3\u679c\u5982\u4e0b\u3002\r\n \r\n create table ihome_area2\r\n(\r\n create_time datetime null,\r\n update_time datetime null,\r\n id int auto_increment\r\n primary key,\r\n name varchar(32) not null\r\n);\r\n\r\n \"\"\"\r\n\r\n enginex = create_engine(\r\n 'mysql+pymysql://root:123456@127.0.0.1:3306/aj?charset=utf8',\r\n max_overflow=10, # \u8d85\u8fc7\u8fde\u63a5\u6c60\u5927\u5c0f\u5916\u6700\u591a\u521b\u5efa\u7684\u8fde\u63a5\r\n pool_size=50, # \u8fde\u63a5\u6c60\u5927\u5c0f\r\n pool_timeout=30, # \u6c60\u4e2d\u6ca1\u6709\u7ebf\u7a0b\u6700\u591a\u7b49\u5f85\u7684\u65f6\u95f4\uff0c\u5426\u5219\u62a5\u9519\r\n pool_recycle=3600, # \u591a\u4e45\u4e4b\u540e\u5bf9\u7ebf\u7a0b\u6c60\u4e2d\u7684\u7ebf\u7a0b\u8fdb\u884c\u4e00\u6b21\u8fde\u63a5\u7684\u56de\u6536\uff08\u91cd\u7f6e\uff09\r\n echo=True)\r\n sqla_helper = SqlaReflectHelper(enginex)\r\n Ihome_area2 = sqla_helper.base_classes.ihome_area2 # ihome_area2\u662f\u8868\u540d\u3002\r\n\r\n\r\n def f1():\r\n with sqla_helper.session as ss:\r\n ss # type: _SessionContext\r\n\r\n print(ss)\r\n\r\n print(ss.query(sqlalchemy.func.count(Ihome_area2.id)).scalar())\r\n\r\n # \u4f7f\u7528orm\u65b9\u5f0f\u63d2\u5165\r\n ss.add(Ihome_area2(create_time=datetime.now(), update_time=datetime.now(), name='testname'))\r\n\r\n print(ss.query(sqlalchemy.func.count(Ihome_area2.id)).scalar())\r\n\r\n # \u4f7f\u7528\u5360\u4f4d\u7b26\u8bed\u6cd5\u63d2\u5165\uff0c\u6b64\u79cd\u53ef\u4ee5\u9632\u6b62sql\u6ce8\u5165\r\n ss.execute(f'''INSERT INTO ihome_area2 (create_time, update_time, name) VALUES (:v1,:v2,:v3)''', params={'v1': '2020-06-14 19:15:14', 'v2': '2020-06-14 19:15:14', 'v3': 'testname00'})\r\n\r\n # \u76f4\u63a5\u81ea\u5df1\u62fc\u63a5\u5b8c\u6574\u5b57\u7b26\u4e32\uff0c\u4e0d\u4f7f\u7528\u4e09\u65b9\u5305\u5360\u4f4d\u7b26\u7684\u540e\u9762\u7684\u53c2\u6570\uff0c\u6b64\u79cd\u4f1a\u5f15\u8d77sql\u6ce8\u5165\uff0c\u4e0d\u63a8\u8350\u3002\r\n cur = ss.execute(f'''INSERT INTO ihome_area2 (create_time, update_time, name) VALUES ('2020-06-14 19:15:14','2020-06-14 19:15:14', 'testname')''', )\r\n\r\n # \u8fd9\u6837\u4e5f\u53ef\u4ee5\u6253\u5370\u6267\u884c\u7684\u8bed\u53e5\r\n # noinspection PyProtectedMember\r\n print(cur._saved_cursor._executed)\r\n\r\n # \u4f7f\u7528\u6700\u539f\u751f\u7684\u8bed\u53e5\uff0c\u76f4\u63a5\u8c03\u7528\u4e86pymysql\u7684cursor\u5bf9\u8c61\u3002\r\n conny = sqla_helper.engine.raw_connection()\r\n cury = conny.cursor(DictCursor) # type: DictCursor\r\n print(cury)\r\n cury.execute('SELECT * FROM ihome_area2 LIMIT 3')\r\n result = cury.fetchall()\r\n print(result)\r\n conny.commit()\r\n cury.close()\r\n conny.close()\r\n\r\n\r\n def f2():\r\n ss = sqla_helper.get_session_factory()()\r\n print(ss)\r\n print(ss.query(sqlalchemy.func.count(sqla_helper.base_classes.ihome_area.id)).scalar())\r\n ss.add(sqla_helper.base_classes.ihome_area(create_time=datetime.now(), update_time=datetime.now(), name='testname'))\r\n ss.commit()\r\n print(ss.query(sqlalchemy.func.count(sqla_helper.base_classes.ihome_area.id)).scalar())\r\n ss.close()\r\n\r\n\r\n with decorator_libs.TimerContextManager():\r\n t_pool = BoundedThreadPoolExecutor(10) # \u5c01\u88c5mysql\uff0c\u5207\u8bb0\u4e00\u5b9a\u8981\u6d4b\u8bd5\u591a\u7ebf\u7a0b\u4e0b\u7684\u60c5\u51b5\u3002\r\n for _ in range(500):\r\n # f1()\r\n t_pool.submit(f1)\r\n t_pool.shutdown()\r\n\r\n\r\n```\r\n\r\n![](https://visitor-badge.glitch.me/badge?page_id=db_libs)\r\n",
"bugtrack_url": null,
"license": "BSD License",
"summary": "'redis', 'mongo', 'elasticsearch', 'mysql', 'sqlachemy', '\u7ebf\u7a0b\u5b89\u5168\u7684\u6570\u636e\u5e93\u5c01\u88c5\uff0c\u4eab\u5143\u6a21\u5f0f\u652f\u6301\u65e0\u9650\u5b9e\u4f8b\u5316\u8c03\u7528'",
"version": "1.0",
"project_urls": null,
"split_keywords": [
"database",
"redis",
"mongo",
"elasticsearch",
"mysql",
"sqlachemy"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "3a7c0a73b99ceb16a69b6b79a6be209fc5dbab614aa7dbb787443398b9175719",
"md5": "eec32f0bcca76501c461ab0154602f76",
"sha256": "cd38d10ae65b337c2b4970c80d05f994d67ddb592efd13f0bc6402e088e63e61"
},
"downloads": -1,
"filename": "db_libs-1.0.tar.gz",
"has_sig": false,
"md5_digest": "eec32f0bcca76501c461ab0154602f76",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 13229,
"upload_time": "2024-03-18T02:24:49",
"upload_time_iso_8601": "2024-03-18T02:24:49.111480Z",
"url": "https://files.pythonhosted.org/packages/3a/7c/0a73b99ceb16a69b6b79a6be209fc5dbab614aa7dbb787443398b9175719/db_libs-1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-03-18 02:24:49",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "db-libs"
}