这是一个简易的使用
Python
语言连接Mysql
数据库的连接池
。
This is a simple
connection pool
that usesPython
language to connect toMysql
database.
-
Themis(忒弥斯) 取名来自于古希腊神话中秩序女神的名字, 就如同连接池的作用一样, 管理所有用户的连接, 减少不必要的损耗。
-
Themis is named after the name of the goddess of order in ancient Greek mythology, just like the role of the connection pool, it manages the connections of all users and reduces unnecessary losses.
your python project
|
|
|-- util
|
|-- db.cnf
|
|-- ThemisPool.py
- 我们需要在与
ThemisPool.py
同级文件夹下有用一个后缀名为.cnf
的配置文件。 - We need to have a configuration file with the suffix
.cnf
in the same folder asThemisPool.py
.
# db.cnf file
[mysql]
host = localhost
user = root
password = 12345678
database = practice
initsize = 3
maxsize = 6
- 在db.cnf中我们需要对将要连接的
mysql数据库
和ThemisPool连接池
做一些基本配置。 - In db.cnf we need to do some basic configuration of the
mysql database
andThemisPool connection pool
to be connected.
参数 (Attribute) |
说明 (Description) |
类型 (Type) |
默认值 (Default) |
---|---|---|---|
host | 连接数据库的地址 The address to connect to the database |
String | localhost |
port | 端口号 | int | 3306 |
user | 连接数据库的用户名 User name to connect to the database |
String | root |
password | 连接数据库的密码 Password to connect to the database |
String | - |
database | 本次需要连接的数据库 The database that needs to be connected this time |
String | - |
initsize | 连接池初始化连接数 Number of initial connections in connection pool |
int | - |
maxsize | 连接池可以同时存在的最大连接数 The maximum number of connections that the connection pool can exist at the same time |
int | - |
from util.ThemisPool import ThemisPool
# 初始化ThemisPool连接池 (Initialize the ThemisPool connection pool)
db = ThemisPool()
# 查询拉取数据,函数会直接返回数据 (Query pull data.It returns data directly)
selectSql = "select * from user;"
data = db.fetchone(selectSql)
# 增、删、改语句, 如果有使用mysql自增长插入的值函数会返回自增长的数据 (insert,upate delete and alter. If there is a value function inserted using mysql self-growth, it will return self-growth data)
insertSql = "insert into user values(null,'user001','123456')"
id = db.update(selectSql)
ThemisPool
支持开发者自定义配置文件和配置文件中的区块名!ThemisPool
supports developers to customize configuration files and sections in configuration files!
- 我们的配置文件取名为:
myDB.cnf
- Our configuration file is named:
myDB.cnf
# myDB.cnf file
# 配置文件内的区块名为: "volcano"
# The sections in the configuration file is: 'volcano'
[volcano]
host = localhost
user = root
password = 12345678
database = practice
initsize = 3
maxsize = 6
- 那么我们可以在初始化时进行声明
- Then we can declare at initialization
from util.ThemisPool import ThemisPool
# 声明配置文件名和配置块名
# Declare the configuration file name and sections
db = ThemisPool(fileName='myDB.cnf', configName='volcano')
The above is all the content of this time. The next version will solve the problem that Python cannot format the data of type datetime
with json format
, and integrate it!
感谢您的浏览
Thank You For Browsing!