一个用于简化PostgreSQL数据库操作的Python工具包
Project description
PgSQL Utils
一个用于简化 PostgreSQL 数据库操作的 Python 工具包。
功能特点
- 单例模式数据库连接管理
- 支持多种配置方式(环境变量、配置文件、直接传参)
- 自动序列重置功能,避免主键冲突
- 支持字典类型数据自动转换为 JSON 字符串
- 提供多种便捷的数据操作方法:
find: 查询数据add: 插入单条数据add_batch: 批量插入数据add_smart: 智能插入(冲突时更新)update: 更新数据delete: 删除数据execute: 执行 SQL 语句execute_query: 执行查询语句execute_update: 执行更新语句
安装
pip install pgsql-utils
使用方法
-
基本使用
from pgsql_utils import PostgreSQLUtils # 方式1: 直接传参 db = PostgreSQLUtils("localhost", "mydb", "user", "password", 5432) # 方式2: 从环境变量读取配置 # 需要设置环境变量: PGSQL_IP, PGSQL_DB, PGSQL_USER_NAME, PGSQL_USER_PASS, PGSQL_PORT db = PostgreSQLUtils() # 方式3: 从配置文件读取 db = PostgreSQLUtils.from_config_file("config.json") # 方式4: 从设置模块读取 import your_setting_module db = PostgreSQLUtils.from_settings(your_setting_module) # 方式5: 直接使用预定义的全局实例(需要先配置环境变量或配置文件) from pgsql_utils import db
-
查询数据
# 查询数据并以字典形式返回 results = db.find("SELECT * FROM news_table WHERE category_id = %s", True, 1) for result in results: print(result)
-
插入数据
# 插入单条数据 data = { "title": "新闻标题", "content": "新闻内容", "publish_time": "2023-01-01 12:00:00" } db.add("news_table", data) # 批量插入数据 data_list = [ {"title": "新闻1", "content": "内容1"}, {"title": "新闻2", "content": "内容2"} ] db.add_batch("news_table", data_list)
-
智能插入(冲突时更新)
data = { "id": 1, "title": "更新的标题", "content": "更新的内容" } db.add_smart("news_table", data)
-
更新和删除数据
# 更新数据 db.update("news_table", {"title": "新标题"}, "id = %s", (1,)) # 删除数据 db.delete("news_table", "id = %s", (1,))
配置
-
环境变量配置
export PGSQL_IP=localhost export PGSQL_DB=your_database export PGSQL_USER_NAME=your_username export PGSQL_USER_PASS=your_password export PGSQL_PORT=5432
-
JSON配置文件示例
{ "host": "localhost", "database": "your_database", "user": "your_username", "password": "your_password", "port": 5432 }
-
设置模块文件示例
# PostgreSQL PGSQL_IP = "host" PGSQL_PORT = 5432 PGSQL_DB = "your_database" PGSQL_USER_NAME = "your_username" PGSQL_USER_PASS = "your_password"
依赖
- psycopg2>=2.9.10
- colorPrintConsole>=1.0.7
许可证
Apache-2.0 License
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pgsql_utils-1.0.1.tar.gz
(13.2 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pgsql_utils-1.0.1.tar.gz.
File metadata
- Download URL: pgsql_utils-1.0.1.tar.gz
- Upload date:
- Size: 13.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0da40a6ed8a1123a9b77cd9cda05e6c33e0a38df03719e43fae6da64934ce725
|
|
| MD5 |
21abab3c6823a8d517325afc78c3c059
|
|
| BLAKE2b-256 |
c2b4c897fdc94d41ab734aaaeed03e328a15d52f9c31c53592cd2728d693acf6
|
File details
Details for the file pgsql_utils-1.0.1-py3-none-any.whl.
File metadata
- Download URL: pgsql_utils-1.0.1-py3-none-any.whl
- Upload date:
- Size: 12.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
41c677c4d14d9b7742dc98b9d45a4c560b37bbed7979ee317ca00160db55daeb
|
|
| MD5 |
947dfa3ffbd381233498f5a98d6c0d38
|
|
| BLAKE2b-256 |
8eb27fdc10a3ae491601a954e9a2336ce1f6733266f8facd537f354b5ab96adf
|