Spaces:
Running
Running
Robin Chiu
commited on
Commit
·
7ec1d7a
1
Parent(s):
295aacc
support sqlite multi-command.
Browse files- utils/tools.py +17 -6
utils/tools.py
CHANGED
|
@@ -67,14 +67,25 @@ def execute_sqlite_query(db_name, query):
|
|
| 67 |
conn = sqlite3.connect(tmp_db_file)
|
| 68 |
cursor = conn.cursor()
|
| 69 |
|
| 70 |
-
#
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
# 獲取結果
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
# 關閉連接
|
| 80 |
conn.close()
|
|
|
|
| 67 |
conn = sqlite3.connect(tmp_db_file)
|
| 68 |
cursor = conn.cursor()
|
| 69 |
|
| 70 |
+
# 將查詢按分號分開並執行每條查詢
|
| 71 |
+
queries = query.split(';')
|
| 72 |
+
for q in queries:
|
| 73 |
+
q = q.strip()
|
| 74 |
+
if q: # 確保不執行空查詢
|
| 75 |
+
cursor.execute(q)
|
| 76 |
|
| 77 |
# 獲取結果
|
| 78 |
+
try:
|
| 79 |
+
rows = cursor.fetchall()
|
| 80 |
+
# 獲取列名
|
| 81 |
+
columns = [description[0] for description in cursor.description] if cursor.description else []
|
| 82 |
+
except sqlite3.Error:
|
| 83 |
+
# 如果最後一個語句不是 SELECT,則沒有結果可獲取
|
| 84 |
+
rows = []
|
| 85 |
+
columns = []
|
| 86 |
+
|
| 87 |
+
# 提交事務
|
| 88 |
+
conn.commit()
|
| 89 |
|
| 90 |
# 關閉連接
|
| 91 |
conn.close()
|