Skip to main content

简单的数据库连接

Project description

说明

简单的数据库连接

用法

pip install myally_database

推荐搭配包 python-decouple 以便可以直接在 .env 中写配置

创建个 database.py

pip install python-decouple

from decouple import config
from myally_database import Database
db_config = {
    'user': config('DB_USER'),
    'password': config('DB_PASSWORD'),
    'host': config('DB_HOST'),
    'port': config('DB_PORT', default=3306, cast=int),
    'database': config('DB_DATABASE')
}
# 创建一个全局的Database实例,可以在其他文件中直接使用
database = Database(db_config)

.env 配置

DB_USER=名
DB_PASSWORD=数据库密码
DB_HOST=ip
DB_PORT=端口
DB_DATABASE=数据库

调用后用法与包 SQLAlchemy 一致

from database import database
# 获取数据库会话
session = database.get_session()
# 提交事务
session.commit()
# 关闭会话
session.close()

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

myally_database-1.3.tar.gz (1.7 kB view details)

Uploaded Source

Built Distribution

myally_database-1.3-py3-none-any.whl (2.0 kB view details)

Uploaded Python 3

File details

Details for the file myally_database-1.3.tar.gz.

File metadata

  • Download URL: myally_database-1.3.tar.gz
  • Upload date:
  • Size: 1.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.0 CPython/3.10.6 Windows/10

File hashes

Hashes for myally_database-1.3.tar.gz
Algorithm Hash digest
SHA256 3bc1f717d313122eec9f2cc5533b3b56a6fad04043b3bd0050995a8eeb5286b8
MD5 0ca46d98ab9a3b21941aa86d082879af
BLAKE2b-256 7cc5f852ec9b596b7dd9f68fd2427e7e4c6d0639e4dbf0b5d813844dc5003870

See more details on using hashes here.

File details

Details for the file myally_database-1.3-py3-none-any.whl.

File metadata

  • Download URL: myally_database-1.3-py3-none-any.whl
  • Upload date:
  • Size: 2.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.0 CPython/3.10.6 Windows/10

File hashes

Hashes for myally_database-1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 f28925df4553ae0bcfdac98d0bb39c95d2b1b79b755e41ec08f6e001aaa1bc16
MD5 a9f813c3f5ff49b898cad66d38970251
BLAKE2b-256 b4b90767da4f2ba9eaa1750f10074afb32bcd1b2409cff9138989a7f717f38ce

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