Package: zdpapi-mysql
zapi_mysql基于异步的快速操作MySQL的组件项目地址：https://github.com/zhangdapeng520/zdpapi_mysql使用pip安装pipinstallzapi_mysql一、增删改数据1.1 创建表importasynciofromzapi_mysqlimportMysqldb=Mysql(host='127.0.0.1',port=3306,user='root',password='root',db='test')asyncdeftest_example_execute(loop):# 删除表awaitdb.connect()sql="DROP TABLE IF EXISTS user;"# 创建表awaitdb.execute(sql)sql="""CREATE TABLE user(id INT,name VARCHAR(255),PRIMARY KEY (id));"""awaitdb.execute(sql)# 插入SQL语句sql="INSERT INTO user VALUES(1,'张三')"awaitdb.execute(sql)loop=asyncio.get_event_loop()loop.run_until_complete(test_example_execute(loop))1.2 插入数据importasynciofromzapi_mysqlimportMysqldb=Mysql(host='127.0.0.1',port=3306,user='root',password='root',db='test')asyncdeftest_example_execute(loop):# 插入SQL语句sql="INSERT INTO user VALUES(2,'李四')"awaitdb.execute(sql)loop=asyncio.get_event_loop()loop.run_until_complete(test_example_execute(loop))1.3 批量插入数据importasynciofromzapi_mysqlimportMysqldb=Mysql(host='127.0.0.1',port=3306,user='root',password='root',db='test')asyncdeftest_example_execute(loop):# 插入SQL语句data=[(4,'gothic metal'),(5,'doom metal'),(6,'post metal')]sql="INSERT INTO user VALUES(%s,%s)"awaitdb.execute(sql,data=data)loop=asyncio.get_event_loop()loop.run_until_complete(test_example_execute(loop))二、查询数据2.1 查询所有数据importasynciofromzapi_mysqlimportMysqldb=Mysql(host='127.0.0.1',port=3306,user='root',password='root',db='test')asyncdeftest_example_execute(loop):# 插入SQL语句sql="SELECT id, name FROM user ORDER BY id"result=awaitdb.execute(sql)print("查询结果：\n",result)loop=asyncio.get_event_loop()loop.run_until_complete(test_example_execute(loop))2.2 查询单条数据importasynciofromzapi_mysqlimportMysqldb=Mysql(host='127.0.0.1',port=3306,user='root',password='root',db='test')asyncdeftest_example_execute(loop):# 查询单条数据sql="SELECT id, name FROM user ORDER BY id"result=awaitdb.execute(sql,return_all=False)print("查询结果：\n",result)loop=asyncio.get_event_loop()loop.run_until_complete(test_example_execute(loop))三、CRUD快捷工具3.1 新增用户importasynciofromzdpapi_mysqlimportMysql,Cruddb=Mysql(host='127.0.0.1',port=3306,user='root',password='root',db='test')crud=Crud(db,"user",["name"])asyncdeftest_create_table(loop):# 删除表awaitdb.connect()sql="DROP TABLE IF EXISTS user;"# 创建表awaitdb.execute(sql)sql="""CREATE TABLE user(id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255));"""awaitdb.execute(sql)# 插入SQL语句sql="INSERT INTO user VALUES(1,'张三')"awaitdb.execute(sql)asyncdeftest_insert(loop):# 插入SQL语句awaitcrud.add("李四")awaitcrud.add("王五")awaitcrud.add("赵六")if__name__=="__main__":loop=asyncio.get_event_loop()loop.run_until_complete(test_create_table(loop))loop.run_until_complete(test_insert(loop))3.2 添加多条数据importasynciofromzdpapi_mysqlimportMysql,Cruddb=Mysql(host='127.0.0.1',port=3306,user='root',password='root',db='test')crud=Crud(db,"user",["name"])asyncdeftest_create_table(loop):# 删除表awaitdb.connect()sql="DROP TABLE IF EXISTS user;"# 创建表awaitdb.execute(sql)sql="""CREATE TABLE user(id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255));"""awaitdb.execute(sql)# 插入SQL语句sql="INSERT INTO user VALUES(1,'张三')"awaitdb.execute(sql)asyncdeftest_insert(loop):# 插入SQL语句awaitcrud.add("李四")awaitcrud.add("王五")awaitcrud.add("赵六")asyncdeftest_insert_many(loop):# 插入SQL语句data=[("孙悟空",),("猪八戒",),("沙僧",),]awaitcrud.add_many(data)if__name__=="__main__":loop=asyncio.get_event_loop()loop.run_until_complete(test_create_table(loop))loop.run_until_complete(test_insert(loop))loop.run_until_complete(test_insert_many(loop))3.3 根据ID删除数据importasynciofromzdpapi_mysqlimportMysql,Cruddb=Mysql(host='127.0.0.1',port=3306,user='root',password='root',db='test')crud=Crud(db,"user",["name"])asyncdeftest_create_table(loop):# 删除表awaitdb.connect()sql="DROP TABLE IF EXISTS user;"# 创建表awaitdb.execute(sql)sql="""CREATE TABLE user(id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255));"""awaitdb.execute(sql)# 插入SQL语句sql="INSERT INTO user VALUES(1,'张三')"awaitdb.execute(sql)asyncdeftest_insert(loop):# 插入SQL语句awaitcrud.add("李四")awaitcrud.add("王五")awaitcrud.add("赵六")asyncdeftest_insert_many(loop):# 插入SQL语句data=[("孙悟空",),("猪八戒",),("沙僧",),]awaitcrud.add_many(data)asyncdeftest_delete(loop):# 根据ID删除数据awaitcrud.delete(1)awaitcrud.delete(2)awaitcrud.delete(3)if__name__=="__main__":loop=asyncio.get_event_loop()loop.run_until_complete(test_create_table(loop))loop.run_until_complete(test_insert(loop))loop.run_until_complete(test_insert_many(loop))loop.run_until_complete(test_delete(loop))3.4 根据ID列表删除importasynciofromzdpapi_mysqlimportMysql,Cruddb=Mysql(host='127.0.0.1',port=3306,user='root',password='root',db='test')crud=Crud(db,"user",["name"])asyncdeftest_create_table(loop):# 删除表awaitdb.connect()sql="DROP TABLE IF EXISTS user;"# 创建表awaitdb.execute(sql)sql="""CREATE TABLE user(id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255));"""awaitdb.execute(sql)# 插入SQL语句sql="INSERT INTO user VALUES(1,'张三')"awaitdb.execute(sql)asyncdeftest_insert(loop):# 插入SQL语句awaitcrud.add("李四")awaitcrud.add("王五")awaitcrud.add("赵六")asyncdeftest_insert_many(loop):# 插入SQL语句data=[("孙悟空",),("猪八戒",),("沙僧",),]awaitcrud.add_many(data)asyncdeftest_delete(loop):# 根据ID删除数据awaitcrud.delete(1)awaitcrud.delete(2)awaitcrud.delete(3)asyncdeftest_delete_ids(loop):# 根据ID列表删除数据awaitcrud.delete_ids((3,4,5))if__name__=="__main__":loop=asyncio.get_event_loop()loop.run_until_complete(test_create_table(loop))loop.run_until_complete(test_insert(loop))loop.run_until_complete(test_insert_many(loop))loop.run_until_complete(test_delete(loop))loop.run_until_complete(test_delete_ids(loop))3.5 更新多条数据importasynciofromzdpapi_mysqlimportMysql,Cruddb=Mysql(host='127.0.0.1',port=3306,user='root',password='root',db='test')crud=Crud(db,"user",["name"])asyncdeftest_create_table(loop):# 删除表awaitdb.connect()sql="DROP TABLE IF EXISTS user;"# 创建表awaitdb.execute(sql)sql="""CREATE TABLE user(id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255));"""awaitdb.execute(sql)# 插入SQL语句sql="INSERT INTO user VALUES(1,'张三')"awaitdb.execute(sql)asyncdeftest_insert(loop):# 插入SQL语句awaitcrud.add("李四")awaitcrud.add("王五")awaitcrud.add("赵六")asyncdeftest_insert_many(loop):# 插入SQL语句data=[("孙悟空",),("猪八戒",),("沙僧",),]awaitcrud.add_many(data)asyncdeftest_delete(loop):# 根据ID删除数据awaitcrud.delete(1)awaitcrud.delete(2)awaitcrud.delete(3)asyncdeftest_delete_ids(loop):# 根据ID列表删除数据awaitcrud.delete_ids((3,4,5))asyncdeftest_update(loop):# 根据ID更新数据awaitcrud.update(6,{"name":"二郎神111"})asyncdeftest_update_many(loop):# 更新多条数据data=[{"id":6,"name":"猪八戒"},{"id":7,"name":"嫦娥"},]awaitcrud.update_many(data)if__name__=="__main__":loop=asyncio.get_event_loop()loop.run_until_complete(test_create_table(loop))loop.run_until_complete(test_insert(loop))loop.run_until_complete(test_insert_many(loop))loop.run_until_complete(test_delete(loop))loop.run_until_complete(test_delete_ids(loop))loop.run_until_complete(test_update(loop))loop.run_until_complete(test_update_many(loop))3.6 查询单条数据importasynciofromzdpapi_mysqlimportMysql,Cruddb=Mysql(host='127.0.0.1',port=3306,user='root',password='root',db='test')crud=Crud(db,"user",["name"])asyncdeftest_create_table(loop):# 删除表awaitdb.connect()sql="DROP TABLE IF EXISTS user;"# 创建表awaitdb.execute(sql)sql="""CREATE TABLE user(id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255));"""awaitdb.execute(sql)# 插入SQL语句sql="INSERT INTO user VALUES(1,'张三')"awaitdb.execute(sql)asyncdeftest_insert(loop):# 插入SQL语句awaitcrud.add("李四")awaitcrud.add("王五")awaitcrud.add("赵六")asyncdeftest_insert_many(loop):# 插入SQL语句data=[("孙悟空",),("猪八戒",),("沙僧",),]awaitcrud.add_many(data)asyncdeftest_delete(loop):# 根据ID删除数据awaitcrud.delete(1)awaitcrud.delete(2)awaitcrud.delete(3)asyncdeftest_delete_ids(loop):# 根据ID列表删除数据awaitcrud.delete_ids((3,4,5))asyncdeftest_update(loop):# 根据ID更新数据awaitcrud.update(6,{"name":"二郎神111"})asyncdeftest_update_many(loop):# 更新多条数据data=[{"id":6,"name":"猪八戒"},{"id":7,"name":"嫦娥"},]awaitcrud.update_many(data)asyncdeftest_find_one(loop):# 查询单条数据result=awaitcrud.find(6)print("查询结果：\n",result)if__name__=="__main__":loop=asyncio.get_event_loop()loop.run_until_complete(test_create_table(loop))loop.run_until_complete(test_insert(loop))loop.run_until_complete(test_insert_many(loop))loop.run_until_complete(test_delete(loop))loop.run_until_complete(test_delete_ids(loop))loop.run_until_complete(test_update(loop))loop.run_until_complete(test_update_many(loop))loop.run_until_complete(test_find_one(loop))3.8 根据ID列表查询importasynciofromzdpapi_mysqlimportMysql,Cruddb=Mysql(host='127.0.0.1',port=3306,user='root',password='root',db='test')crud=Crud(db,"user",["name"])asyncdeftest_create_table(loop):# 删除表awaitdb.connect()sql="DROP TABLE IF EXISTS user;"# 创建表awaitdb.execute(sql)sql="""CREATE TABLE user(id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255));"""awaitdb.execute(sql)# 插入SQL语句sql="INSERT INTO user VALUES(1,'张三')"awaitdb.execute(sql)asyncdeftest_insert(loop):# 插入SQL语句awaitcrud.add("李四")awaitcrud.add("王五")awaitcrud.add("赵六")asyncdeftest_insert_many(loop):# 插入SQL语句data=[("孙悟空",),("猪八戒",),("沙僧",),]awaitcrud.add_many(data)asyncdeftest_delete(loop):# 根据ID删除数据awaitcrud.delete(1)awaitcrud.delete(2)awaitcrud.delete(3)asyncdeftest_delete_ids(loop):# 根据ID列表删除数据awaitcrud.delete_ids((3,4,5))asyncdeftest_update(loop):# 根据ID更新数据awaitcrud.update(6,{"name":"二郎神111"})asyncdeftest_update_many(loop):# 更新多条数据data=[{"id":6,"name":"猪八戒"},{"id":7,"name":"嫦娥"},]awaitcrud.update_many(data)asyncdeftest_find_one(loop):# 查询单条数据result=awaitcrud.find(6)print("查询结果：\n",result)asyncdeftest_find_ids(loop):# 根据ID列表查询result=awaitcrud.find_ids([6,7,8])print("查询结果：\n",result)if__name__=="__main__":loop=asyncio.get_event_loop()loop.run_until_complete(test_create_table(loop))loop.run_until_complete(test_insert(loop))loop.run_until_complete(test_insert_many(loop))loop.run_until_complete(test_delete(loop))loop.run_until_complete(test_delete_ids(loop))loop.run_until_complete(test_update(loop))loop.run_until_complete(test_update_many(loop))loop.run_until_complete(test_find_one(loop))loop.run_until_complete(test_find_ids(loop))3.9 分页查询数据importasynciofromzdpapi_mysqlimportMysql,Cruddb=Mysql(host='127.0.0.1',port=3306,user='root',password='root',db='test')crud=Crud(db,"user",["name"])asyncdeftest_create_table(loop):# 删除表awaitdb.connect()sql="DROP TABLE IF EXISTS user;"# 创建表awaitdb.execute(sql)sql="""CREATE TABLE user(id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255));"""awaitdb.execute(sql)# 插入SQL语句sql="INSERT INTO user VALUES(1,'张三')"awaitdb.execute(sql)asyncdeftest_insert(loop):# 插入SQL语句awaitcrud.add("李四")awaitcrud.add("王五")awaitcrud.add("赵六")asyncdeftest_insert_many(loop):# 插入SQL语句data=[("孙悟空",),("猪八戒",),("沙僧",),]awaitcrud.add_many(data)asyncdeftest_delete(loop):# 根据ID删除数据awaitcrud.delete(1)awaitcrud.delete(2)awaitcrud.delete(3)asyncdeftest_delete_ids(loop):# 根据ID列表删除数据awaitcrud.delete_ids((3,4,5))asyncdeftest_update(loop):# 根据ID更新数据awaitcrud.update(6,{"name":"二郎神111"})asyncdeftest_update_many(loop):# 更新多条数据data=[{"id":6,"name":"猪八戒"},{"id":7,"name":"嫦娥"},]awaitcrud.update_many(data)asyncdeftest_find_one(loop):# 查询单条数据result=awaitcrud.find(6)print("查询结果：\n",result)asyncdeftest_find_ids(loop):# 根据ID列表查询result=awaitcrud.find_ids([6,7,8])print("查询结果：\n",result)asyncdeftest_find_page(loop):# 分页查询数据result=awaitcrud.find_page(1,20)print("查询结果：\n",result)if__name__=="__main__":loop=asyncio.get_event_loop()loop.run_until_complete(test_create_table(loop))loop.run_until_complete(test_insert(loop))loop.run_until_complete(test_insert_many(loop))loop.run_until_complete(test_delete(loop))loop.run_until_complete(test_delete_ids(loop))loop.run_until_complete(test_update(loop))loop.run_until_complete(test_update_many(loop))loop.run_until_complete(test_find_one(loop))loop.run_until_complete(test_find_ids(loop))loop.run_until_complete(test_find_page(loop))3.10 查询总数importasynciofromzdpapi_mysqlimportMysql,Cruddb=Mysql(host='127.0.0.1',port=3306,user='root',password='root',db='test')crud=Crud(db,"user",["name"])asyncdeftest_create_table(loop):# 删除表awaitdb.connect()sql="DROP TABLE IF EXISTS user;"# 创建表awaitdb.execute(sql)sql="""CREATE TABLE user(id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255));"""awaitdb.execute(sql)# 插入SQL语句sql="INSERT INTO user VALUES(1,'张三')"awaitdb.execute(sql)asyncdeftest_insert(loop):# 插入SQL语句awaitcrud.add("李四")awaitcrud.add("王五")awaitcrud.add("赵六")asyncdeftest_insert_many(loop):# 插入SQL语句data=[("孙悟空",),("猪八戒",),("沙僧",),]awaitcrud.add_many(data)asyncdeftest_delete(loop):# 根据ID删除数据awaitcrud.delete(1)awaitcrud.delete(2)awaitcrud.delete(3)asyncdeftest_delete_ids(loop):# 根据ID列表删除数据awaitcrud.delete_ids((3,4,5))asyncdeftest_update(loop):# 根据ID更新数据awaitcrud.update(6,{"name":"二郎神111"})asyncdeftest_update_many(loop):# 更新多条数据data=[{"id":6,"name":"猪八戒"},{"id":7,"name":"嫦娥"},]awaitcrud.update_many(data)asyncdeftest_find_one(loop):# 查询单条数据result=awaitcrud.find(6)print("查询结果：\n",result)asyncdeftest_find_ids(loop):# 根据ID列表查询result=awaitcrud.find_ids([6,7,8])print("查询结果：\n",result)asyncdeftest_find_page(loop):# 分页查询数据result=awaitcrud.find_page(1,20)print("查询结果：\n",result)asyncdeftest_find_total(loop):# 查询数据总数result=awaitcrud.find_total()print("查询结果：\n",result)if__name__=="__main__":loop=asyncio.get_event_loop()loop.run_until_complete(test_create_table(loop))loop.run_until_complete(test_insert(loop))loop.run_until_complete(test_insert_many(loop))loop.run_until_complete(test_delete(loop))loop.run_until_complete(test_delete_ids(loop))loop.run_until_complete(test_update(loop))loop.run_until_complete(test_update_many(loop))loop.run_until_complete(test_find_one(loop))loop.run_until_complete(test_find_ids(loop))loop.run_until_complete(test_find_page(loop))loop.run_until_complete(test_find_total(loop))