1- """
2- Make database file for trle.net and TRCustoms.org
3- """
1+ """Make database file for trle.net and TRCustoms.org."""
42import sqlite3
53import json
64import os
4442 duration INT,
4543 type INT NOT NULL,
4644 class INT,
47- trleID INT,
48- trcustomsID INT,
45+ trleID INT UNIQUE ,
46+ trcustomsID INT UNIQUE ,
4947 FOREIGN KEY (difficulty) REFERENCES InfoDifficulty(InfoDifficultyID),
5048 FOREIGN KEY (duration) REFERENCES InfoDuration(InfoDurationID),
5149 FOREIGN KEY (type) REFERENCES InfoType(InfoTypeID),
@@ -76,7 +74,7 @@ class INT,
7674 name TEXT NOT NULL UNIQUE,
7775 size FLOAT NOT NULL,
7876 md5sum TEXT NOT NULL,
79- url TEXT,
77+ url TEXT UNIQUE ,
8078 version INT,
8179 release DATE
8280)''' )
@@ -123,7 +121,7 @@ class INT,
123121CREATE TABLE Level (
124122 LevelID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
125123 body TEXT NOT NULL,
126- walkthrough TEXT NOT NULL,
124+ walkthrough TEXT NOT NULL,
127125 infoID INTEGER NOT NULL,
128126 FOREIGN KEY (infoID) REFERENCES Info(InfoID)
129127)''' )
@@ -230,10 +228,8 @@ class INT,
230228
231229# Add Game File data
232230for i in range (1 , 6 ):
233- file = f'data/fileList-TR{ i } .json'
234-
235231 # Load data from JSON file
236- with open (file , 'r' , encoding = 'utf-8' ) as json_file :
232+ with open (f'data/fileList-TR { i } .json' , 'r' , encoding = 'utf-8' ) as json_file :
237233 file_info = json .load (json_file )
238234
239235 name = file_info .get ("name" )
@@ -249,41 +245,55 @@ class INT,
249245
250246 if relative_path and file_md5 :
251247 # Check if the file with the same md5sum already exists in File table
252- C .execute ("SELECT FileID FROM File WHERE md5sum = ? AND path = ?" ,
253- (file_md5 , relative_path ))
248+ C .execute (
249+ "SELECT FileID FROM File WHERE md5sum = ? AND path = ?" ,
250+ (file_md5 , relative_path )
251+ )
254252 existing_file = C .fetchone ()
255253
256254 if existing_file :
257255 # File already exists, use the existing FileID
258256 file_id = existing_file [0 ]
259- print ("File with md5sum %s and path %s already exists. Using existing FileID: %s" ,
260- file_md5 , relative_path , file_id )
257+ print (
258+ "File with md5sum %s and path %s already exists. Using existing FileID: %s" ,
259+ file_md5 ,
260+ relative_path ,
261+ file_id
262+ )
261263 else :
262264 # File doesn't exist, insert it into File table
263- C .execute ("INSERT INTO File (md5sum, path) VALUES (?, ?)" ,
264- (file_md5 , relative_path ))
265+ C .execute (
266+ "INSERT INTO File (md5sum, path) VALUES (?, ?)" ,
267+ (file_md5 , relative_path )
268+ )
265269 file_id = C .lastrowid
266270 print (f"Inserted new file with md5sum { file_md5 } . New FileID: { file_id } " )
267271
268- try :
269- # Check if the combination of fileID and gameID already exists in GameFileList
270- C .execute ("SELECT 1 FROM GameFileList WHERE fileID = ? AND gameID = ?" ,
271- (file_id , game_id ))
272- existing_combination = C .fetchone ()
273-
274- if not existing_combination :
275- # Combination doesn't exist, insert it into GameFileList
276- C .execute ("INSERT INTO GameFileList (fileID, gameID) VALUES (?, ?)" ,
277- (file_id , game_id ))
278- else :
279- # Combination already exists, print a message or handle it as needed
280- print (f"Combination of FileID { file_id } and LevelID { game_id } "
281- "already exists in GameFileList. Skipping insertion." )
282-
283- except sqlite3 .IntegrityError as file_list_error :
284- # Print more details about the uniqueness violation
285- print (f"Uniqueness violation in GameFileList: { file_list_error } " )
286- print (f"FileID: { file_id } , LevelID: { game_id } " )
272+ try :
273+ # Check if the combination of fileID and gameID already exists in GameFileList
274+ C .execute (
275+ "SELECT 1 FROM GameFileList WHERE fileID = ? AND gameID = ?" ,
276+ (file_id , game_id )
277+ )
278+ existing_combination = C .fetchone ()
279+
280+ if not existing_combination :
281+ # Combination doesn't exist, insert it into GameFileList
282+ C .execute (
283+ "INSERT INTO GameFileList (fileID, gameID) VALUES (?, ?)" ,
284+ (file_id , game_id )
285+ )
286+ else :
287+ # Combination already exists, print a message or handle it as needed
288+ print (
289+ f"Combination of FileID { file_id } and LevelID { game_id } "
290+ "already exists in GameFileList. Skipping insertion."
291+ )
292+
293+ except sqlite3 .IntegrityError as file_list_error :
294+ # Print more details about the uniqueness violation
295+ print (f"Uniqueness violation in GameFileList: { file_list_error } " )
296+ print (f"FileID: { file_id } , LevelID: { game_id } " )
287297
288298CONNECTION .commit ()
289299CONNECTION .close ()
0 commit comments