Package: pymysql-manager
pymysql connection & pool manager for python3Refactor pymysql connectionNew featuresParameter ‘charset’ default is utf8Parameter ‘autocommit’ default is TrueAdded parameter ‘timezone’, default is ‘+00:00’Use pymysql.cursors.DictCursor by defaultReconnect after the database connection is lostAdd logs for creating connections, mysql warnings, exceptions, database queries, etc.Using the with…as syntax for transaction operationsProvide simplified query methods such as fetch_all/fetch_row/fetch_column/fetch_firstProvide simplified methods such as insert/insert_many/update/delete1. Create pymysql connectionimportpymysqlfrompymysql_managerimportConnectionconn=Connection(host='192.0.0.1',database='foo',timezone='+8:00')2. TransactionBefore code:try:conn.begin()conn.execute(....)catchException:conn.rollback()else:conn.commit()Now:withconn.transaction():conn.execute(...)3. Fetch rowsets# executed: select * from foo where id between 5 and 10all_rows=conn.fetch_all('select * from foo where id between%sand%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)4. Fetch by IteratorWhen a result is large, it may be usedSSCursor. But sometimes usinglimit … offset …can reduce the pressure on the databaseby SSCursorcursor=conn.cursor(pymysql.cursors.SSCursor)conn.execute(sql)whileTrue:row=cursor.fetchone()ifnotrow:breakby fetch_iteratorforrowinconn.fetch_iterator(sql,per=1000,max=100000):print(row)5. Single/Bulk Insert or Replace | Update | Delete# 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%sand%s',10,5,foo=1,bar=2)db.update('update mytable where id between%sand%s',[10,5],foo=1,bar=2)db.update('update mytable where id between%sand%s',*[10,5],**dict(foo=1,bar=2))# update from mytable where id between %s and %sdb.delete('delete from mytable id between%sand%s',10,5)db.delete('delete from mytable id between%sand%s',[10,5])Connection Pool1. Create connection poolfrompymysql_managerimportConnectionPooledpooled=ConnectionPooled(host='192.0.0.1',database='foo',pool_options=dict(max_size=10,max_usage=100000,idle=60,ttl=120))2. Execute SQL without the connection poolpooled.execute(sql)pooled.connection.execute(sql)3. Using connection pool to execute SQLwithpooled.pool()asconnection:connection.execute(sql)Connection Manager1. Configurationfrompymysql_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. Get a connectionm.execute(sql)# use default connectionm['foo].execute(sql)m.connection('foo').exeucte(sql)3. Get a connection from connection poolwithm.pool()asconnection:pass# use default connectionwithm['foo'].pool()asconnection:passwithm.connection('foo').pool()asconnection:passLicenseThe MIT License (MIT). Please see License File for more information.