
db/index.py
python
import threading
import sqlite3
thread_local_data = threading.local()
# 测试数据库连接
def test():
conn = sqlite3.connect("E:/temp/m-yuying-db/my_yuying_dev_local.db")
cu = conn.cursor()
cu.execute("select * from user")
data = cu.fetchall()
total = len(data)
print("data", total)
# 获取数据库连接
def handleDbConnection():
if not hasattr(thread_local_data, "connection"):
thread_local_data.connection = sqlite3.connect(
"E:/temp/m-yuying-db/my_yuying_dev_local.db", check_same_thread=False
)
return thread_local_data.connection
light/user/index.py
python
from flask import request, jsonify
from db.index import test, handleDbConnection
def userSearch():
req = request.get_json()
print(req)
# test()
conn = handleDbConnection()
cu = conn.cursor()
cu.execute("select * from user")
data = cu.fetchall()
total = len(data)
print("data2", total)
return jsonify(
{"code": 200, "data": {"name": req["name"], "total": total}, "msssage": "成功"}
)
