Skip to main content

No project description provided

Project description

dbinterface

Build Status Coverage Status

Python 连接数据库,执行读写操作的通用接口,采用简单工厂模式,基于接口和组合进行编程。

安装

git clone  https://github.com/somenzz/dbinterface.git 
pip install -r requirements.txt
pip install dbinterface

使用方法

读取数据

from dbinterface.database_client import DataBaseClientFactory

client1 = DataBaseClientFactory.create(
            dbtype="postgres",
            host="localhost",
            port=5432,
            user="postgres",
            pwd="121113",
            database="postgres",
        )
result1 = client1.read("select current_date")
for row in result1:
    #do something
    pass


client = DataBaseClientFactory.create(
            dbtype="mysql",
            host="localhost",
            port=3306,
            user="aaron",
            pwd="aaron",
            database="information_schema",
        )
result2 = client.read("select current_date()")

for row in result2:
    #do something
    pass

更新数据

from dbinterface.database_client import DataBaseClientFactory

client1 = DataBaseClientFactory.create(
            dbtype="postgres",
            host="localhost",
            port=5432,
            user="postgres",
            pwd="121113",
            database="postgres",
        )

rows_affeted = client1.write(
    "insert into tmp_test_table values(%s, %s)", ("1", "aaron")
)
assert rows_affeted == 1

导出数据

from dbinterface.database_client import DataBaseClientFactory

client = DataBaseClientFactory.create(
            dbtype="postgres",
            host="localhost",
            port=5432,
            user="postgres",
            pwd="121113",
            database="postgres",
        )

rows_export = client.export(
            "select * from information_schema.TABLES",
            params=(),
            file_path="/Users/aaron/tmp/mysql_tables.txt",
            delimeter="0x02",
            quote="0x03",
            all_col_as_str=False,
        )

assert rows_export > 0

获取表名列表

from dbinterface.database_client import DataBaseClientFactory

client = DataBaseClientFactory.create(
            dbtype="postgres",
            host="localhost",
            port=5432,
            user="postgres",
            pwd="121113",
            database="postgres",
        )

table_list = client.get_tables()
for table in table_list:
    print(table['name'])
    print(table['type'])
    print(table['schema'])
    print(table['remarks'])

扩展

  • 修改 database_interface.py 文件,添加新的函数

  • 添加新的数据库,以 oracle 为例,在 dbinterface 目录下,新增 oracle_client.py 文件,参考其他 client 编写即可。

  • 然后修改 dbinterface 目录下 database_client.py 文件,修改 DataBaseClientFactory 类,添加对应的数据库类型。

  • tests 目录内添加单元测试,执行 pytess 测试

联系我

微信号 somenzz 公众号 「Python七号」

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

dbinterface-0.5.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

dbinterface-0.5-py3-none-any.whl (19.0 kB view details)

Uploaded Python 3

File details

Details for the file dbinterface-0.5.tar.gz.

File metadata

  • Download URL: dbinterface-0.5.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.5

File hashes

Hashes for dbinterface-0.5.tar.gz
Algorithm Hash digest
SHA256 0b4be859dddfd1d61947952e3c8236fe9b42bd37c1eeb40bcbe99a94a4b3f600
MD5 f5c55e704da00b074dbe5feb891682a7
BLAKE2b-256 d94425c0102a6e48d25bf2fe97f9c989e8d6c236eafdd3d4f8b43e0f3357ad00

See more details on using hashes here.

File details

Details for the file dbinterface-0.5-py3-none-any.whl.

File metadata

  • Download URL: dbinterface-0.5-py3-none-any.whl
  • Upload date:
  • Size: 19.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.5

File hashes

Hashes for dbinterface-0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 91ad4c570a8a34e5390a894eee2b91bfe829fe4c76d5bb8058f0bb2a6de208df
MD5 c7089d9a354ddc34ad833d4715dd3550
BLAKE2b-256 ab2c0dbd9dbb652de77df286ef3b29fa487f526eb33bf7eab622cc8b23569614

See more details on using hashes here.

Supported by

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