Skip to main content

db-hammer

[中文] [English]

db-hammer 是个简单数据库工具库,在 Python DB-API上提供一些高级方法;方便大家经常做数据库查询。 提供方便的导出数据功能,可高性能、稳定的导出超大表数据。 目前已对 MySQL、Oracle、MSSQL、PostgreSQL 数据进行封装,封装其他结构化数据库也很简单。

安装

pip3 install db-hammer

Python Console

>>> from db_hammer.mysql import MySQLConnection

>>> db_conf = {"host": "10.0.0.10","user": "dbuser","pwd": "dbpassword","database": "database"}
>>> db = MySQLConnection(**db_conf):
>>> r = db.select_dict_list("select * from t_student")
>>> r
[{'name': '小明', 'sex': '男', 'age': 18, 'address': '湖南省长沙岳麓区', 'mobile': '13012345678'}, {'name': '小花', 'sex': '女', 'age': 16, 'address': '江苏省南京市鼓楼区', 'mobile': '13100000001'}]

Python With

from db_hammer.mysql import MySQLConnection

db_conf = {"host": "10.0.0.10","user": "dbuser","password": "dbpassword","database": "database"}

with MySQLConnection(**db_conf) as db:
    i_sql = db.gen_insert_dict_sql(dict_data={
        "name": "小白",
        "sex": "男",
        "age": "20",
        "address": "上海市虹口区",
    }, table_name="t_student")

    db.execute(i_sql)
    db.commit()

    rs = db.select_dict_list(sql="select * from t_student")
    print(rs)

连接其他数据库

from db_hammer.oracle import OracleConnection
from db_hammer.postgresql import PostgreSQLConnection
from db_hammer.mssql import MsSQLConnection

db_oracle = OracleConnection(**db_conf)

db_psql = PostgreSQLConnection(**db_conf)

db_mssql = MsSQLConnection(**db_conf)

导出数据库数据

with MySQLConnection(**mysql_conf) as db:
    db.export_data_file(sql="select * from exam_rule_b", dir_path="./export_data", file_mode="csv")

根据SQL:select * from exam_rule_b导出到./export_data,默认为分割文件,导出文件类型支持:txt、csv、gz,可以传入文件编码格式:encoding

游标方式导出,实际测试大表导出,20个G都没有问题。且导出性能较高,使用gz格式导出时,自动压缩,且很多工具支持直接读取gz文本。

开启日志

import logging
logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S')

常用数据库方法列表

# 根据sql获取页数
select_page_size(sql: str, page_size=50)

# 获取分页列表数据,列表字典形式返回
select_page_list(sql: str, page_size=50, page_start=1, **kwargs)

# 获取分页列表数据,列表字典形式返回
select_dict_page_list(sql: str, page_size=50, page_start=1, **kwargs)

# 获取第一列第一行的值
select_value(sql: str)

# 获取列表数据,列表方式返回
select_list(sql: str)

# 获取所有列表数据,列表字典形式返回
select_dict_list(sql: str)

# 获取第一行数据,字典形式返回
select_dict(sql: str)

# 执行SQL,并返回影响行数
execute(sql: str)

# 关闭连接
close()

# 回滚事务
rollback()

# 提交事务
commit()

# 根据字典生成 Insert SQL
gen_insert_dict_sql(dict_data: dict, table_name: str)

# 根据字典来生成 Update SQL
gen_update_dict_sql(dict_data: dict, table_name: str, where: str)

# 根据sql语句把数据生成 Insert SQL
select_insert_sql(sql: str, table_name: str)

其他小工具

# 执行本地命令
db_hammer.util.cmd
# 简单的日期转换
db_hammer.util.date
# 获取文件和目录工具(好用)
db_hammer.util.file
# 简单保存键值工具
db_hammer.util.keep
# 一个简单的收发邮件工具
db_hammer.util.sim_email

Metadata

Release files for db-hammer 0.0.60

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for db-hammer 0.0.60
File Size Uploaded Provenance
db-hammer-0.0.60.tar.gz 59.6 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for db-hammer 0.0.60
File Interpreter ABI Platform
db_hammer-0.0.60-py3-none-any.whl Python 3 none any Details

Total release size: 126.8 kB

Release files / db-hammer-0.0.60.tar.gz

Download URL db-hammer-0.0.60.tar.gz
Size 59.6 kB
Tags Source
SHA-256 checksum
How to use checksums
e5ff2e09d1a981f7894754b82e7bc2296287d0e72911875783dd13305d311dcb
BLAKE2b-256 checksum
How to use checksums
a75307cea1f92353e1355f5eb3bbdf12d6c6222b5dcdc7e14fbe06ab14043e86
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.7

Release files / db_hammer-0.0.60-py3-none-any.whl

Download URL db_hammer-0.0.60-py3-none-any.whl
Size 67.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
93eb3d9031cfbb342624a40c83877d494966c096143324082499831c4b5ce008
BLAKE2b-256 checksum
How to use checksums
aae165f1c17c465da01b51fb6e468771be24d670a765f10f99e09922ff12e8d7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.7

Security for db-hammer 0.0.60

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.60 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page