灵感来源-php 框架的sql查询操作
写的不好。。。欢迎指正!!!
先上调用的代码:
db_result = db.update(
table="platform_interface_list",
field={
"response": finally_resp,
"status": status,
"req_time": req_time,
"resp_time": resp_time
},
where={"id =": inter_id}
)
result = Db().insert(
table="platform_business_list",
field={
"name": name,
"description": description,
"number": 0,
"status": 2,
"create_time": int(time.time()),
"admin": admin
}
)
article = db.select(
field=["a.`id`", "a.`title`", "a.`description`"],
table="article a INNER JOIN member AS m ON a.mid = m.id",
where=__where,
other="ORDER BY a.id DESC limit {}, {}".format((int(page_num) - 1) * int(page_size), page_size)
)
封装的代码:
import json
import sys
import pymysql
from configuration import *
class Db(object):
def __init__(self):
try:
self.configure = pymysql.connect(
host=DB_HOST,
user=DB_USER,
password=DB_PASSWORD,
database=DB_DATABASE
)
self.begin = self.configure.cursor(pymysql.cursors.DictCursor)
except pymysql.err.OperationalError:
sys.exit()
except pymysql.err.InternalError:
sys.exit()
# select查询语句
def select(self, field: list, table: str, where: dict = None, other: str = ""):
"""
:param field: 需要查询的字段名称 [str, str, ...]
:param table: 表名,如需多表联查请在表名后面加上 => "table_name as `c` inner join table_name as b ..."
:param where: 查询条件,非必填 => {"name =": "alex", " OR title like": "'%{}%'".format(title), ...}
:param other: 其他跟在where后面的条件
:return: ({"key": "value", ...} ...)
"""
_field = ",".join(field)
if where: