-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
81 lines (69 loc) · 2.53 KB
/
main.py
File metadata and controls
81 lines (69 loc) · 2.53 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import sqlite3
import sys
import os
def run_sql_file(sql_file):
sql_file = os.path.abspath(sql_file)
db_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "database.db")
print("SQL file:", sql_file)
print("Database file:", db_path)
conn = None
try:
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
with open(sql_file, "r", encoding="utf-8") as f:
script = f.read()
if not script.strip():
print("⚠️ SQL file is empty")
return
# First attempt: run the full script
try:
cursor.executescript(script)
conn.commit()
print(f"✅ Successfully executed: {os.path.basename(sql_file)}")
return
except sqlite3.Error as e:
print("\n❌ Error occurred. Trying to locate failing statement...\n")
print("SQLite error:", e)
print("--------------------------------------------")
# Verbose statement-by-statement check
current_stmt = ""
in_string = False
string_char = ""
for line_no, line in enumerate(script.splitlines(), start=1):
stripped = line.strip()
i = 0
while i < len(line):
char = line[i]
if char in ('"', "'"):
if not in_string:
in_string = True
string_char = char
elif char == string_char:
in_string = False
if char == ";" and not in_string:
current_stmt += char
try:
cursor.execute(current_stmt)
except sqlite3.Error as e:
print(f"❌ SQL Error at line {line_no}")
print("--------------------------------------------")
print(current_stmt.strip())
print("--------------------------------------------")
print("SQLite error message:", e)
return
current_stmt = ""
else:
current_stmt += char
i += 1
current_stmt += "\n"
conn.commit()
except Exception as e:
print("\n⚠️ Unexpected Python error:", e)
finally:
if conn:
conn.close()
if __name__ == "__main__":
if len(sys.argv) < 2:
print("No SQL file provided.")
else:
run_sql_file(sys.argv[1])