Skip to main content

A simple database API

Project description

pydbapi

Installation

pip install pydbapi

支持的数据库类型

  • sqlite
from pydbapi import SqliteDB
db = SqliteDB(database=None) #或者传入路径
sql = 'select * from [table];'
row, action, result = db.execute(sql)
  • Amazon Redshift
from pydbapi import RedshiftDB
db = RedshiftDB(host, user, password, database, port='5439')
sql = 'select * from [schema].[table];'
row, action, result = db.execute(sql)
  • Mysql
from pydbapi import MysqlDB
db = MysqlDB(host, user, password, database, port='3306')
sql = 'select * from [table];'
row, action, result = db.execute(sql)
  • Snowflake
from pydbapi import SnowflakeDB
db = SnowflakeDB(user, password, account, warehouse, database, schema)
sql = 'select * from [table];'
row, action, result = db.execute(sql)

支持的操作

  • execute【db/base.py】
    • 代码
      db.execute(sql, count=None, verbose=None)
    • params
      • count: 返回结果的数量;
      • verbose: 是否打印执行进度。
  • select
    • 代码
      db.select(tablename, columns, condition=None)
    • params
      • tablename: 表名;
      • columns: 列内容; Example: {'id_rename': {'sqlexpr': 'id', 'func': 'min', 'order': 1}, ……}
        • sqlexpr: sql表达式
        • func: 后续处理的函数
        • order: 用于排序
      • condition: sql where 中的条件
  • create
    • 代码
      db.create(tablename, columns, indexes=None)
    • params
      • tablename: 表名;
      • columns: 列内容; Example: {'id': 'integer', 'name': 'varchar', 'address': 'varchar(1024)'}
      • indexes: 索引,sqlite暂不支持索引
  • insert【db/base.py】
    • 代码
      db.insert(tablename, columns, values)
    • params
      • tablename: 表名;
      • columns: 列内容; Example: {'id': 'integer', 'name': 'varchar', 'address': 'varchar(1024)'}
      • values: 插入的数值;
  • drop【db/base.py】
    • 代码
      db.drop(tablename)
    • params
      • tablename: 表名;
  • delete【db/base.py】
    • 代码
      db.delete(tablename, condition)
    • params
      • tablename: 表名;
      • condition: 插入的数值;
  • get_columns
    • 代码
      db.get_columns(tablename)
    • params
      • tablename: 表名;
  • add_columns
    • 代码
      db.add_columns(tablename, columns)
    • params
      • tablename: 表名;
      • columns: 列内容; Example: {'id': 'integer', 'name': 'varchar', 'address': 'varchar(1024)'}
  • get_filesqls【db/fileexec.py】
    • 代码
      db.get_filesqls(filepath, **kw)
    • params
      • filepath: sql文件路径;
      • kw: sql文件中需要替换的参数,会替换sqlfile中的arguments;
  • file_exec【db/fileexec.py】
    • 代码
      db.file_exec(filepath, **kw)
    • params
      • filepath: sql文件路径; 文件名以test开始或者结尾会打印sql执行的步骤;
      • kw: sql文件中需要替换的参数 在sql文件中用$param, 会替换sqlfile中的arguments;
    • sql文件格式(在desc中增加verbose会打印sql执行的步骤;)
      #arguments#
      ts = '2020-06-28'
      date = today
      date_max = date + timedelta(days=10)
      #arguments#
      ###
      --【desc1 [verbose]】 #sql描述
      --step1
      sql1;
      --step2
      sql2 where name = $name;
      ###
      ###
      --【desc2 [verbose]】 #sql描述
      --step1
      sql1;
      --step2
      sql2;
      ###
      
    • arguments
      • 支持python表达式(datetime、date、timedelta)
      • 支持全局变量和当前sqlfile设置过的变量
      • now:获取执行的时间
      • today: 获取执行的日期

支持的的settings【conf/settings.py】

  • AUTO_RULES
    可以自动执行表名(表名包含即可)
  • REDSHIFT_AUTO_RULES
    Amazon Redshift 可以自动执行表名(表名包含即可)

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

pydbapi-0.0.32.tar.gz (14.0 kB view details)

Uploaded Source

Built Distribution

pydbapi-0.0.32-py3-none-any.whl (20.4 kB view details)

Uploaded Python 3

File details

Details for the file pydbapi-0.0.32.tar.gz.

File metadata

  • Download URL: pydbapi-0.0.32.tar.gz
  • Upload date:
  • Size: 14.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.9.0

File hashes

Hashes for pydbapi-0.0.32.tar.gz
Algorithm Hash digest
SHA256 a855f62c2eab02fdb240c586349f6344e1fcbf9612d526a0d53b3594e4439b20
MD5 8ad768c2a2e0b3d67119ab931463c49f
BLAKE2b-256 559801b119cd7e6dfb4ac869370a3dca5cbd3be3e6c9c0be72b78ceab0191bd0

See more details on using hashes here.

Provenance

File details

Details for the file pydbapi-0.0.32-py3-none-any.whl.

File metadata

  • Download URL: pydbapi-0.0.32-py3-none-any.whl
  • Upload date:
  • Size: 20.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.9.0

File hashes

Hashes for pydbapi-0.0.32-py3-none-any.whl
Algorithm Hash digest
SHA256 97a68a24097e720e72fa074d5e7b6214911edf0cd4467bc21bdb4c29fd52b64d
MD5 1bb29eb9add475a830add97daea867cc
BLAKE2b-256 71f8c0ad93dec58f58cf6b394539e9d51cdb3ba7d3acb2fdcee4271c13970d65

See more details on using hashes here.

Provenance

Supported by

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