database tools;数据库操作
Project description
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
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
db-hammer-0.0.60.tar.gz
(59.6 kB
view details)
Built Distribution
File details
Details for the file db-hammer-0.0.60.tar.gz
.
File metadata
- Download URL: db-hammer-0.0.60.tar.gz
- Upload date:
- Size: 59.6 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e5ff2e09d1a981f7894754b82e7bc2296287d0e72911875783dd13305d311dcb |
|
MD5 | 843cefd2cca9b1e58288a321ca0e05c7 |
|
BLAKE2b-256 | a75307cea1f92353e1355f5eb3bbdf12d6c6222b5dcdc7e14fbe06ab14043e86 |
File details
Details for the file db_hammer-0.0.60-py3-none-any.whl
.
File metadata
- Download URL: db_hammer-0.0.60-py3-none-any.whl
- Upload date:
- Size: 67.2 kB
- Tags: Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 93eb3d9031cfbb342624a40c83877d494966c096143324082499831c4b5ce008 |
|
MD5 | a83d448fd7a52b3de5d781bbe44a4532 |
|
BLAKE2b-256 | aae165f1c17c465da01b51fb6e468771be24d670a765f10f99e09922ff12e8d7 |