Created
November 23, 2023 02:21
-
-
Save mlouielu/be8573026faac2d3bab71707eb574c36 to your computer and use it in GitHub Desktop.
Restore Darktable Image Ratings from Snapshot Database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlite3 | |
def get_column(source_db_path, table_name, column_name): | |
sc = sqlite3.connect(source_db_path) | |
cursor = sc.cursor() | |
cursor.execute(f"SELECT id, filename, {column_name} FROM {table_name}") | |
rows = cursor.fetchall() | |
cursor.close() | |
return rows | |
def update_rating(cursor, table_name, column_name, row_id, rating): | |
cursor.execute( | |
f""" | |
UPDATE {table_name} | |
SET {column_name} = ? | |
WHERE id = ? | |
""", | |
(rating, row_id), | |
) | |
def main(): | |
# Example usage | |
source_db = "library.db.from" # Path to source database file | |
target_db = "library.db.target" # Path to target database file | |
table = "images" # Table name | |
column = "flags" # Column name to be copied | |
a = get_column(source_db, table, column) | |
b = get_column(target_db, table, column) | |
tc = sqlite3.connect(target_db) | |
cursor = tc.cursor() | |
for pk, filename, flags in a: | |
rating = flags & 0b1111 | |
update_rating(cursor, table, column, pk, rating) | |
tc.commit() | |
tc.close() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment