Skip to main content

pgsql数据库工具

Project description

使用说明

本程序包用到的库:psycopg3 官方文档:https://www.psycopg.org/psycopg3/docs/advanced/pool.html#other-ways-to-create-a-pool

1. 下载安装

pip install pgmanage

2. 导入包模块

3.使用示例

  • 示例1
from pgmanage import Dbmanager
<!-- import Dbmanager -->

dbm = Dbmanager()
with dbm.pool.connection() as conn:
    with conn.cursor() as cur:
        cur.execute("SELECT 1")
        data = cur.fetchone()
        print(data)
# 使用完成后会自动关闭连接池
  • 示例2
from pgmanage import Pgtool

# 设置日志
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

def main():

    # 生成10万行的测试数据
    data_to_bulk_insert = []
    for i in range(100000):
        data_to_bulk_insert.append({'id': f'{i}', 'name': f'name{i}88','name2': f'name{i}88', 'age': i % 100, 'email': f'email{i}@example.com','fasle': True})


    # 创建数据库管理器实例
    db_mgr = Pgtool()

    # 写入数据,主键冲突时更新所有非主键列 upsert_data
    rows_inserted = db_mgr.upsert_data(
        schema='data_test', 
        table='table_test', 
        data=data_to_bulk_insert,
        batch_size=10000,
        update=True,
        create=True,
        add_columns=True,
        max_workers=10,
    )
    logger.info(f"总共插入 {rows_inserted} 行")

    # 执行 SQL 查询
    query = "SELECT * FROM data_test.table_test LIMIT 10;"
    result = db_mgr.exec_query(query)
    for row in result:
        logger.info(row)
        
    # 删除数据
    query = "delete  FROM data_test.table_test;"
    result = db_mgr.exec_query_rowcount(query)
    logger.info(f"删除结果:{result}")

if __name__ == '__main__':
    main()

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

pgmanage-1.0.1.tar.gz (12.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pgmanage-1.0.1-py3-none-any.whl (14.1 kB view details)

Uploaded Python 3

File details

Details for the file pgmanage-1.0.1.tar.gz.

File metadata

  • Download URL: pgmanage-1.0.1.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.13.1

File hashes

Hashes for pgmanage-1.0.1.tar.gz
Algorithm Hash digest
SHA256 96e93aa8e25a5f7503f2983642905e1f5cd4ce3de768c43f291bf81846e64068
MD5 bace4b7256a93b3781b8cd4e5987737d
BLAKE2b-256 950931f0a7ddc1cc87d0e9d86e9a08e8cb1b810bf46185e457a561569fc902d5

See more details on using hashes here.

File details

Details for the file pgmanage-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: pgmanage-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 14.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.13.1

File hashes

Hashes for pgmanage-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 47e9dba2bd559f853e51d44fba0ca5bcaf52492d8109ce8dd586306097de2a0c
MD5 192df020c766b05aa8d87a13ce935609
BLAKE2b-256 7ac079f7937a25d8576e6e63f4bc23f386f85b77b3322ac8e72284f64e39ccae

See more details on using hashes here.

Supported by

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