pymysql连接和python3池管理器

pymysql-manager的Python项目详细描述


pymysql连接&python3的池管理器

重构pymysql连接

新功能

  1. 参数“charset”默认为utf8
  2. 参数“autocommit”默认值为true
  3. 添加了参数“时区”,默认值为“+00:00”
  4. 默认情况下使用pymysql.cursors.dictcursor
  5. 数据库连接丢失后重新连接
  6. 添加用于创建连接、mysql警告、异常、数据库查询等的日志。
  7. 使用with…作为事务操作的语法
  8. 提供简化的查询方法,例如fetch_all/fetch_row/fetch_column/fetch_first
  9. 提供简单的方法,如插入/插入多个/更新/删除

一。创建pymysql连接

importpymysqlfrompymysql_managerimportConnectionconn=Connection(host='192.0.0.1',database='foo',timezone='+8:00')

2.交易

编码前:

try:conn.begin()conn.execute(....)catchException:conn.rollback()else:conn.commit()

现在:

withconn.transaction():conn.execute(...)

三。获取行集

# executed: select * from foo where id between 5 and 10all_rows=conn.fetch_all('select * from foo where id between %s and %s',5,10)# executed: select * from foo limit 1first_row=conn.fetch_row('select * from foo')# executed: select * from foo limit 1first_column_on_first_row=conn.fetch_first('select * from foo')# executed: select * from foo limit 1third_column_on_first_row=conn.fetch_column('select * from foo',column=3)

四。通过迭代器获取

当结果很大时,可以使用sscursor。但有时使用limit…offset…可以减少对数据库的压力

作者:sscursor

cursor=conn.cursor(pymysql.cursors.SSCursor)conn.execute(sql)whileTrue:row=cursor.fetchone()ifnotrow:break

通过获取迭代器

forrowinconn.fetch_iterator(sql,per=1000,max=100000):print(row)

5个。单个/批量插入或替换更新删除

# insert ignore into mytable (foo, bar) values (1, 2)db.insert('insert ignore into mytable',foo=1,bar=2)# insert ignore into mytable (foo, bar) values (1, 2) on duplicate key update ...db.insert('insert ignore into mytable on duplicate key update ...',**dict(foo=1,bar=2))# insert ignore into mytable (id, name) values (1, 'foo'), (2, 'bar') on duplicate key update ...db.insert_many('insert ignore into mytable on duplicate key update ...',['id','name'],[(1,'foo'),(2,'bar')])# update mytable set foo=1, bar=2 where id between %s and %sdb.update('update mytable where id between %s and %s',10,5,foo=1,bar=2)db.update('update mytable where id between %s and %s',[10,5],foo=1,bar=2)db.update('update mytable where id between %s and %s',*[10,5],**dict(foo=1,bar=2))# update from mytable where id between %s and %sdb.delete('delete from mytable id between %s and %s',10,5)db.delete('delete from mytable id between %s and %s',[10,5])

连接池

一。创建连接池

frompymysql_managerimportConnectionPooledpooled=ConnectionPooled(host='192.0.0.1',database='foo',pool_options=dict(max_size=10,max_usage=100000,idle=60,ttl=120))

2.在没有连接池的情况下执行sql

pooled.execute(sql)pooled.connection.execute(sql)

三。使用连接池执行sql

withpooled.pool()asconnection:connection.execute(sql)

连接管理器

一。配置

frompymysql_managerimportConnectionManagerm=ConnectionManager(default='foo',foo=dict(host='192.0.0.1',database='foo',user='root',passwd=''),bar=dict(host='192.0.0.1',database='bar',user='root',passwd=''))

2.连接

m.execute(sql)# use default connectionm['foo].execute(sql)m.connection('foo').exeucte(sql)

三。从连接池获取连接

withm.pool()asconnection:pass# use default connectionwithm['foo'].pool()asconnection:passwithm.connection('foo').pool()asconnection:pass

许可证

麻省理工学院的执照。有关详细信息,请参阅许可证文件。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java游戏!框架伪造应用程序它实际上做什么?   java如何在JavaFx中显示表视图中的即时更改?   对象类的equals()方法的java重载   xpages介绍如何部署java。IBM Notes中的策略更改   java如何访问侦听器中的另一个视图?   java getDefaultDisplay()的替代方法是什么   java opencv匹配模板   java Android Firebase写入数据时的常量超时   在Java中,如何将包含大量空格的数字字符串转换为一系列Int变量。   带有GUI的swing Java模拟无法运行模拟   java NoSuchElementException在特定的Web端上使用无头铬和硒   java对文件进行迭代,即使文件在目录中也会出现“未找到文件”异常。你能告诉我为什么吗?谢谢   递归Java 8,匿名递归嵌套方法   java为什么我看到枚举常量的字段值会被序列化/反序列化?在哪种情况下,枚举中哪些内容没有序列化?   java在运行sonar scanner和Spotbugs规则时出错,用于单片项目?   java如何检查硬件键盘是否可用?(黑莓)   tile游戏动作侦听器循环中的java错误   sockets Java线程池与高请求场景中的新线程   java如何使用Hibernate注释在联接表上创建索引?