Skip to content

Commit 63c4b00

Browse files
committed
fix delete level
1 parent aa08588 commit 63c4b00

File tree

6 files changed

+163
-139
lines changed

6 files changed

+163
-139
lines changed

database/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
tombll.db.bak
12
data.json
23
file_info.json
34
__pycache__

database/tombll_common.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def get_tombll_json(path):
99
"""Load and parse a JSON file from a specified path.
1010
1111
Attempts to open and read a JSON file, parsing its content into a dictionary.
12-
Handles errors for file not found, JSON decoding issues, and other I/O problems.
12+
Exit on errors for file not found, JSON decoding issues, and other I/O problems.
1313
1414
Args:
1515
path (str): The path to the JSON file.
@@ -52,7 +52,7 @@ def query_return_everything(query, params, con):
5252
5353
Args:
5454
query (str): The SQL query to execute. Expected to be a SELECT or similar.
55-
params (tuple): A tuple of parameters to safely bind to the query.
55+
params (tuple or None): A tuple of parameters to safely bind to the query.
5656
con (sqlite3.Connection): An open SQLite database connection.
5757
5858
Returns:
@@ -92,9 +92,12 @@ def query_return_id(query, params, con):
9292
For other queries, it fetches and returns the first integer result, if it exists and is
9393
non-negative.
9494
95+
If a database error occurs, the error is logged, any open transaction is rolled back,
96+
and the program exits with an error status.
97+
9598
Args:
9699
query (str): SQL query to execute.
97-
params (tuple): Parameters for the query.
100+
params (tuple or None): Parameters for the query.
98101
con (sqlite3.Connection): SQLite database connection.
99102
100103
Returns:
@@ -105,8 +108,12 @@ def query_return_id(query, params, con):
105108
"""
106109
cursor = con.cursor()
107110
try:
108-
# Execute the query with provided parameters
109-
cursor.execute(query, params)
111+
if params:
112+
# Execute the query with the given parameters
113+
cursor.execute(query, params)
114+
else:
115+
# Execute the query without parameters
116+
cursor.execute(query)
110117

111118
# Check if it's an INSERT query to return the last inserted row ID
112119
if query.strip().upper().startswith("INSERT"):
@@ -129,18 +136,26 @@ def query_return_id(query, params, con):
129136
def query_run(query, params, con):
130137
"""Execute a SQL query with the provided parameters.
131138
139+
If a database error occurs, the error is logged, any open transaction is rolled back,
140+
and the program exits with an error status.
141+
132142
Args:
133143
query (str): The SQL query to execute.
134-
params (tuple): Parameters to substitute in the SQL query.
144+
params (tuple or None): Parameters to substitute in the SQL query.
135145
con (sqlite3.Connection): SQLite database connection.
136146
137147
Exits:
138148
Logs an error and exits if a database error occurs.
139149
"""
140150
cursor = con.cursor()
141151
try:
142-
# Execute the query with provided parameters
143-
cursor.execute(query, params)
152+
if params:
153+
# Execute the query with the given parameters
154+
cursor.execute(query, params)
155+
else:
156+
# Execute the query without parameters
157+
cursor.execute(query)
158+
144159
except sqlite3.DatabaseError as db_error:
145160
# Log the database error and exit the program
146161
logging.error("Database error occurred: %s", db_error)
@@ -157,6 +172,6 @@ def make_empty_null(value):
157172
Returns:
158173
None if the value is an empty string or exactly 0.0; otherwise, returns the original value.
159174
"""
160-
if value in ("", 0.0):
175+
if value in ("", 0.0, 0):
161176
return None
162177
return value

database/tombll_create.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -291,14 +291,14 @@ def database_info(data, con):
291291
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
292292
'''
293293
arg = (
294-
data.get('title'), # Title of the level
295-
data.get('release'), # Release date
296-
info_difficulty_id, # Retrieved or NULL difficulty ID
297-
info_duration_id, # Retrieved or NULL duration ID
298-
info_type_id, # Retrieved or NULL type ID
299-
info_class_id, # Retrieved or NULL class ID
300-
data.get('trle_id'), # TRLE ID if available
301-
data.get('trcustoms_id') # TRCustoms ID if available
294+
data.get('title'), # Title of the level
295+
data.get('release'), # Release date
296+
info_difficulty_id, # Retrieved or NULL difficulty ID
297+
info_duration_id, # Retrieved or NULL duration ID
298+
info_type_id, # Retrieved or NULL type ID
299+
info_class_id, # Retrieved or NULL class ID
300+
tombll_common.make_empty_null(data.get('trle_id')), # TRLE ID if available
301+
tombll_common.make_empty_null(data.get('trcustoms_id')) # TRCustoms ID if available
302302
)
303303

304304
return tombll_common.query_return_id(query, arg, con)

0 commit comments

Comments
 (0)