diff --git a/database.py b/database.py index 03c6e7f..e59d446 100644 --- a/database.py +++ b/database.py @@ -97,7 +97,7 @@ class DatabaseFunctions: tags = ', '.join([j for j in tags if j]) sql_command_add = ( - "INSERT INTO \ + "INSERT OR REPLACE INTO \ books (Title, Author, Year, Path, ISBN, Tags, Hash, CoverImage) \ VALUES (?, ?, ?, ?, ?, ?, ?, ?)") diff --git a/sorter.py b/sorter.py index 93d6671..29c2e44 100644 --- a/sorter.py +++ b/sorter.py @@ -79,7 +79,7 @@ class BookSorter: # Caching upon closing self.file_list = [i for i in file_list if os.path.exists(i)] self.statistics = [0, (len(file_list))] - self.hashes = [] + self.hashes_and_paths = {} self.mode = mode self.database_path = database_path self.auto_tags = auto_tags @@ -99,15 +99,17 @@ class BookSorter: # Overwrite book if deleted and then re-added # Also fetch the path of the file here - all_hashes = database.DatabaseFunctions( + all_hashes_and_paths = database.DatabaseFunctions( self.database_path).fetch_data( - ('Hash',), + ('Hash', 'Path'), 'books', {'Hash': ''}, 'LIKE') - if all_hashes: - self.hashes = [i[0] for i in all_hashes] + if all_hashes_and_paths: + # self.hashes = [i[0] for i in all_hashes] + self.hashes_and_paths = { + i[0]: i[1] for i in all_hashes_and_paths} def database_position(self, file_hash): position = database.DatabaseFunctions( @@ -139,10 +141,12 @@ class BookSorter: # This should not get triggered in reading mode # IF the file is NOT being loaded into the reader, + # Do not allow addition in case the file - # is already in the database - if self.mode == 'addition' and file_md5 in self.hashes: - return + # is already in the database and it remains at its original path + if self.mode == 'addition' and file_md5 in self.hashes_and_paths: + if self.hashes_and_paths[file_md5] == filename: + return file_extension = os.path.splitext(filename)[1][1:] try: diff --git a/threaded.py b/threaded.py index 65bd0be..9b659b6 100644 --- a/threaded.py +++ b/threaded.py @@ -92,7 +92,6 @@ class BackGroundBookSearch(QtCore.QThread): pathlib.Path(i[0]) for i in data_list if i[3] == QtCore.Qt.Unchecked] def run(self): - def is_wanted(directory): directory_parents = pathlib.Path(directory).parents for i in self.unwanted_directories: