简单的数据库连接
Project description
说明
简单的数据库连接
用法
pip install myally_database
推荐搭配包 python-decouple 以便可以直接在 .env 中写配置
创建个 database.py
pip install 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 配置
.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.1.tar.gz
(1.7 kB
view hashes)
Built Distribution
Close
Hashes for myally_database-1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7421a841a43f613ad23caff4ff8d6a68742046a059147ff89cae28afd7e36c29 |
|
MD5 | 8cd5c8c3dfcb04fda93dc3c5ab5c709e |
|
BLAKE2b-256 | 303d2f76319cd99d48a321a76e20d15ab3baeb77abbfcca77555fc98cd936b32 |