Skip to main content

psql_sqltool

PostgreSQL 版本的 psql_sqltool。本项目由 007gzs/psql_sqltool (基于 pymysql) 改写而来, 全部适配为 PostgreSQL 语法, 底层使用 psycopg2。

作者: 0716gzs (https://github.com/0716gzs) 项目地址: https://github.com/0716gzs/psql_sqltool

特性

  • PostgreSQL 连接池 (fork 安全, 自动归还连接)

  • 链式 SQL 生成器: INSERT / SELECT / UPDATE / DELETE

  • 批量 SQL 生成, 适合把数据落盘成导入脚本

  • ON CONFLICT ... DO UPDATE (对应 MySQL 的 ON DUPLICATE KEY UPDATE)

  • 自增主键 / 唯一冲突的内存去重

安装

pip install psycopg2-binary
python setup.py install

快速上手

from psql_sqltool.postgres_client import PostgresClient

client = PostgresClient(
    host='127.0.0.1',
    port=5432,
    user='postgres',
    password='postgres',
    dbname='demo',
)

# 查询
rows = client.select(table_name='user', wheres={'id': 1})

# 插入 (参数化)
client.insert(
    [{'id': 1, 'name': 'alice'}, {'id': 2, 'name': 'bob'}],
    table_name='user',
    field_list=('id', 'name'),
)

# 插入并冲突更新 (upsert)
client.insert(
    [{'id': 1, 'name': 'alice2'}],
    table_name='user',
    field_list=('id', 'name'),
    on_conflict_fields=('id',),
    on_duplicate_key_update_fields=('name',),
)

# 更新 / 删除
client.update(table_name='user', update_columns={'name': 'alice'}, wheres={'id': 1})
client.delete(table_name='user', wheres={'id': 1})

批量生成 SQL (导出脚本)

from psql_sqltool.sql_gen import GenSqlAutoId

class GenUser(GenSqlAutoId):
    TABLE_NAME = 'user'
    FIELD_LIST = ('id', 'name')

gen = GenUser()
gen.add_item(name='alice')
gen.add_item(name='bob')
for sql in gen.gen_sql():
    print(sql)

与原 MySQL 版的主要差异

  • 标识符引用: 反引号 ` -> 双引号 "

  • ON DUPLICATE KEY UPDATE ... AS __new_data__ -> ON CONFLICT (...) DO UPDATE SET ... EXCLUDED..., 且 PostgreSQL 必须显式指定冲突目标 on_conflict_fields

  • 分页: LIMIT start, size -> LIMIT size OFFSET start

  • INFORMATION_SCHEMA ... AUTO_INCREMENT -> 通过 pg_get_serial_sequence 读取序列

  • 底层驱动: pymysql -> psycopg2

License

BSD-3-Clause

Metadata

Release files for psql-sqltool 0.1.8

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

Source distribution (sdist)

Source distribution for psql-sqltool 0.1.8
File Size Uploaded Provenance Actions
psql_sqltool-0.1.8.tar.gz 14.9 kB Without provenance Details for psql_sqltool-0.1.8.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for psql-sqltool 0.1.8
File Interpreter ABI Platform Actions
psql_sqltool-0.1.8-py3-none-any.whl Python 3 none any Details for psql_sqltool-0.1.8-py3-none-any.whl

Total release size: 27.0 kB

Release files / psql_sqltool-0.1.8.tar.gz

Download URL psql_sqltool-0.1.8.tar.gz
Size 14.9 kB
Tags Source
SHA-256 checksum
How to use checksums
b1a5c1d0bdb463e77ff317371d7e968e8e1651454bfe75071267b9cd636cb953
BLAKE2b-256 checksum
How to use checksums
e60ddf46a91382236ba0039e5126bb9077e0f3624974e65a5e421c651eb579c0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.11

Release files / psql_sqltool-0.1.8-py3-none-any.whl

Download URL psql_sqltool-0.1.8-py3-none-any.whl
Size 12.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
754d02079bb67e69c67a29358c150e985559bf4fac11907439353b0e421bd0af
BLAKE2b-256 checksum
How to use checksums
09db0e4a94c03033272ef1a1ce5172b887ff52032422c0c1c7ab5b728a57eee4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.11

Security for psql-sqltool 0.1.8

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.

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