基于异步的快速操作MySQL的组件
Project description
zapi_mysql
基于异步的快速操作MySQL的组件
使用pip安装
pip install zapi_mysql
一、增删改数据
1.1 创建表
import asyncio
from zapi_mysql import Mysql
db = Mysql(host='127.0.0.1',
port=3306,
user='root',
password='root',
db='test')
async def test_example_execute(loop):
# 删除表
await db.connect()
sql = "DROP TABLE IF EXISTS user;"
# 创建表
await db.execute(sql)
sql = """CREATE TABLE user
(id INT,
name VARCHAR(255),
PRIMARY KEY (id));"""
await db.execute(sql)
# 插入SQL语句
sql = "INSERT INTO user VALUES(1,'张三')"
await db.execute(sql)
loop = asyncio.get_event_loop()
loop.run_until_complete(test_example_execute(loop))
1.2 插入数据
import asyncio
from zapi_mysql import Mysql
db = Mysql(host='127.0.0.1',
port=3306,
user='root',
password='root',
db='test')
async def test_example_execute(loop):
# 插入SQL语句
sql = "INSERT INTO user VALUES(2,'李四')"
await db.execute(sql)
loop = asyncio.get_event_loop()
loop.run_until_complete(test_example_execute(loop))
1.3 批量插入数据
import asyncio
from zapi_mysql import Mysql
db = Mysql(host='127.0.0.1',
port=3306,
user='root',
password='root',
db='test')
async def test_example_execute(loop):
# 插入SQL语句
data = [(4, 'gothic metal'), (5, 'doom metal'), (6, 'post metal')]
sql = "INSERT INTO user VALUES(%s,%s)"
await db.execute(sql, data=data)
loop = asyncio.get_event_loop()
loop.run_until_complete(test_example_execute(loop))
二、查询数据
2.1 查询所有数据
import asyncio
from zapi_mysql import Mysql
db = Mysql(host='127.0.0.1',
port=3306,
user='root',
password='root',
db='test')
async def test_example_execute(loop):
# 插入SQL语句
sql = "SELECT id, name FROM user ORDER BY id"
result = await db.execute(sql)
print("查询结果:\n", result)
loop = asyncio.get_event_loop()
loop.run_until_complete(test_example_execute(loop))
2.2 查询单条数据
import asyncio
from zapi_mysql import Mysql
db = Mysql(host='127.0.0.1',
port=3306,
user='root',
password='root',
db='test')
async def test_example_execute(loop):
# 查询单条数据
sql = "SELECT id, name FROM user ORDER BY id"
result = await db.execute(sql, return_all=False)
print("查询结果:\n", result)
loop = asyncio.get_event_loop()
loop.run_until_complete(test_example_execute(loop))
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
zdpapi_mysql-0.1.0.tar.gz
(62.0 kB
view details)
Built Distribution
File details
Details for the file zdpapi_mysql-0.1.0.tar.gz
.
File metadata
- Download URL: zdpapi_mysql-0.1.0.tar.gz
- Upload date:
- Size: 62.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.7 CPython/3.8.8 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4d1abea4ef11be580581d36cfc179a4accd60b0c7cdcfd1c1562612473ac7d14 |
|
MD5 | 79dde8761f28dfd99fb8d5f042294895 |
|
BLAKE2b-256 | 57ceac46752ab4164b05baa6cf991f6fbd0fbf6de46c614058e2ff7224a77cf7 |
File details
Details for the file zdpapi_mysql-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: zdpapi_mysql-0.1.0-py3-none-any.whl
- Upload date:
- Size: 68.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.7 CPython/3.8.8 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bf12feeb223fa393d56a39053ab55ff68d19ff4bfe6835a5a628a091030ab919 |
|
MD5 | a081c010106bc6b130cc9c1dc5b1b8eb |
|
BLAKE2b-256 | f90f7b2e631282746c0c2dab58d7394641dcb0d86dfa3997bafc6dca2d37c6a8 |