-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
31 lines (22 loc) · 865 Bytes
/
Copy pathdatabase.py
File metadata and controls
31 lines (22 loc) · 865 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from config import database_config
from pymysql import connect
class Database:
def __init__(self):
self.config = database_config
def execute(self, sql):
"""执行 SQL 并返回 {"columns": [列名...], "rows": [结果行...]}。"""
conn = connect(**self.config)
try:
with conn.cursor() as cursor:
cursor.execute(sql)
columns = [desc[0] for desc in cursor.description] if cursor.description else []
result = cursor.fetchall()
finally:
conn.close()
return {"columns": columns, "rows": result}
if __name__ == "__main__":
from result_formatter import format_result
db = Database()
sql = "SELECT * FROM t_user LIMIT 5;" # Replace with your actual table name
result = db.execute(sql)
print(format_result(result))