Spaces:
Sleeping
Sleeping
| import sqlite3 | |
| import json | |
| class Db(): | |
| def __init__(self, db_path): | |
| self.db_path = db_path | |
| # print(self.db_path) | |
| def list_query(self, query): | |
| conn = sqlite3.connect(self.db_path) | |
| cursor = conn.cursor() | |
| cursor.execute(query) | |
| conn.commit() | |
| rows = cursor.fetchall() | |
| # print('results') | |
| # print(results) | |
| cursor.close() | |
| conn.close() | |
| json_rows = [] | |
| # 为每一行结果创建一个字典,并添加到列表中 | |
| for row in rows: | |
| # 使用cursor.description获取列名和相关信息 | |
| columns = dict(zip([col[0] for col in cursor.description], row)) | |
| json_rows.append(columns) | |
| return json_rows | |
| def insert_query(self, query): | |
| conn = sqlite3.connect(self.db_path) | |
| cursor = conn.cursor() | |
| cursor.execute(query) | |
| conn.commit() | |
| rowcount = cursor.rowcount | |
| return f"插入的行数: {rowcount}" | |
| def update_query(self, query): | |
| conn = sqlite3.connect(self.db_path) | |
| cursor = conn.cursor() | |
| cursor.execute(query) | |
| conn.commit() | |
| rowcount = cursor.rowcount | |
| return f"影响的行数: {rowcount}" | |
| # from fastapi import FastAPI | |
| # # 依赖注入,获取 FastAPI 应用实例 | |
| # def get_app() -> FastAPI: | |
| # return FastAPI() | |
| # def execute_query(query): | |
| # CREATE TABLE api_keys ( | |
| # api_key TEXT NOT NULL, | |
| # type TEXT NOT NULL, | |
| # status INTEGER NOT NULL, | |
| # idx INTEGER NOT NULL, | |
| # PRIMARY KEY (api_key, type, status, idx) | |
| # ); |