Skip to main content

pgsql数据库工具

Project description

使用说明

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

1. 下载安装


2. 导入包模块

3.使用示例

  • 示例1
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
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.0.tar.gz (12.1 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.0-py3-none-any.whl (14.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pgmanage-1.0.0.tar.gz
  • Upload date:
  • Size: 12.1 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.0.tar.gz
Algorithm Hash digest
SHA256 800d7029fc31c53a2226e8d5a1323d989268841aace450485c2da35f17472021
MD5 bd7458f02373661246215206ab2553d3
BLAKE2b-256 00883a9c8757b6f514e3ef1c1fc13b21f90fe6c9e4e2554f8fe58021f84d72e1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pgmanage-1.0.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 999b11ffab75fbab14d6bff693ab21f6f6466a1282edfd30d95a2c70f2e7d393
MD5 b5dfe5290b1652cfb019133df3f73908
BLAKE2b-256 fc07ca2911a22a7cc67d94c78bcb113bd9d483d0a66c2a4ebfc1542514f90639

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