Skip to main content

ugly-sql 数据库工具

Project description

ugly-sql

基础使用

数据库连接工具:

import pymysql
from ugly_sql import SessionManager
import logging
import sys
import contextlib

@contextlib.contextmanager
def db_ctx(cfg,tran=False):
    conn=pymysql.connect(**cfg)
    sm=SessionManager(conn)
    try:
        if tran:
            sm.begin()
        yield sm.__enter__()
        sm.__exit__(None,None,None)
    except Exception as e:
        logging.warning("db_ctx error %s",e)
        et,ev,tb=sys.exc_info()
        sm.__exit__(et,ev,tb)
        raise e
    finally:
        conn.close()

基础操作:

from ugly_sql import Table
User = Table("users",("id","name","password"),"id",True)
db_cfg={}
if __name__ == '__main__':
    with db_ctx(db_cfg,True) as db:
        user=db.query(User).filter(User.name=="root",User.password=="***").one()
        print("name= {}\n password= {}".format(user.name,user.password))

扩展工具

DBConsole[简单数据库操作工具(SQL方式)]

  • ugly_sql.ex.DBConsole(self, connect, args=(), kwargs=None, conn_retry=3)

    ​ connect: 创建数据库连接的函数

    ​ args: 创建数据库连接的参数

    ​ kwargs: 创建数据库连接的命名参数

    ​ conn_retry: 创建连接异常的重试次数

import pymysql

from ugly_sql.ex import DBConsole
cfg=dict(
    host='127.0.0.1', port=3306, user='***', passwd='***', charset='utf8',
    db='test'
)
db = DBConsole(pymysql.connect,kwargs=cfg)
  • simple_select(sql, cursor=None, params=(), rows=False, callback=None)

    sql: 执行的SQL语句

    params: SQL语句的参数

    rows: 指示是否返回多条数据

    callback: 处理数据的回调函数,callback(data,exception)

    db.simple_select("select * from application",callback=lambda d,e:d,rows=True)
    
  • simple_update(sql, params=(), cursor=None, commit=False, auto_id=False, catch=None)

    sql: 执行的SQL语句

    param: SQL语句的参数

    commit: 是否commit

    auto_id:自动生成的ID,auto_id为True则返回ID值

    catch: 发生异常时的回调catch(exception),未指定则抛出异常

db.simple_update("update application set enable=1 where id=%s", params=1, catch=lambda e: print(e))

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

ugly_sql-0.0.13-py2.py3-none-any.whl (10.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file ugly_sql-0.0.13-py2.py3-none-any.whl.

File metadata

  • Download URL: ugly_sql-0.0.13-py2.py3-none-any.whl
  • Upload date:
  • Size: 10.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.2

File hashes

Hashes for ugly_sql-0.0.13-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 07650430f1a6d55386d271a1b7b450937490dcb333b3f6b48c9231f123f76dd0
MD5 21d21f40545992ef367764dd59b53448
BLAKE2b-256 c54b4d08195e7fdf2ebf7fb8a5cd73f9ad0fc240e2a3148a17a543e6bd94232a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page