简单的数据库连接
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 hashes)
Built Distribution
Close
Hashes for myally_database-1.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f28925df4553ae0bcfdac98d0bb39c95d2b1b79b755e41ec08f6e001aaa1bc16 |
|
MD5 | a9f813c3f5ff49b898cad66d38970251 |
|
BLAKE2b-256 | b4b90767da4f2ba9eaa1750f10074afb32bcd1b2409cff9138989a7f717f38ce |