From e08b67470393be901b102c85c392eebc01bec695 Mon Sep 17 00:00:00 2001 From: q Date: Thu, 9 Jul 2015 07:54:14 +0300 Subject: [PATCH] db export to make files smaller --- image_list.py | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/image_list.py b/image_list.py index 18654c1..df3de07 100755 --- a/image_list.py +++ b/image_list.py @@ -33,6 +33,8 @@ def setup_options(): help="Depth of summarization for --du.") parser.add_argument("--exportDesc",action="store",dest="export_descriptions",default=None, help="Walk through folders, and write "+DESCFILE+" in each folder. Format descriptions with {desc} {width}x{height} {red} {green} {blue} {Bred} {Bgreen} {Bblue} {size} {date} {name} {tags}") + parser.add_argument("--export",action="store",dest="exportfile",default=None, + help="Export database to new sqlite database.") parser.add_argument("-f",action="store",dest="sqlfile",default=SQLFILE, help="SQL file name to use [%(default)s]") parser.add_argument("-i",action="store",dest="importfile",default=None, @@ -85,7 +87,8 @@ def setup_options(): options.diskused: options.add=not options.add if options.tag or\ - options.importfile: + options.importfile or\ + options.exportfile: options.add=False return options @@ -98,8 +101,8 @@ def createdb(sqlfile): width INTEGER,height INTEGER,\ fingerprint TEXT,sharpness NUMERIC,\ R REAL,G REAL, B REAL, BR REAL, BG REAL, BB REAL)') - db.execute('CREATE TABLE list (file TEXT,hash TEXT,date INTEGER,size INTEGER)') - db.execute('CREATE TABLE tags (tag TEXT,hash TEXT)') + db.execute('CREATE TABLE list (file TEXT PRIMARY KEY,hash TEXT,date INTEGER,size INTEGER)') + db.execute('CREATE TABLE tags (hash TEXT PRIMARY KEY,tag TEXT)') conn.commit() return @@ -957,6 +960,25 @@ def import_metadata(options): print("Imported %d metadata, %d tags." % (count,tagsafter-tagsbefore)) +def export_database(options): + """ export data to new sqlite file. Minimize file size of sqlite.""" + if not os.path.exists(options.exportfile): + createdb(options.exportfile) + + conn=sqlite3.connect(options.sqlfile) + conn.text_factory=str + db=conn.cursor() + db.execute("ATTACH ? as toDB", (options.exportfile, )) + db.execute("INSERT OR REPLACE INTO toDB.list SELECT * FROM main.list") + db.execute("INSERT OR REPLACE INTO toDB.data SELECT * FROM main.data") + db.execute("INSERT OR IGNORE INTO toDB.tags SELECT * FROM main.tags") + conn.commit() + count=db.execute("SELECT COUNT(hash) FROM toDB.list").fetchall()[0][0] + metacount=db.execute("SELECT COUNT(hash) FROM toDB.data").fetchall()[0][0] + tagscount=db.execute("SELECT COUNT(hash) FROM toDB.tags").fetchall()[0][0] + + print("Exported %d files, %d metadata, %d tags." % (count,metacount,tagscount)) + def check_path(path,opt): """ return relative path name to DB if real path doesnt exist """ if os.path.isfile(path): @@ -1036,6 +1058,9 @@ def main(): if options.importfile: print("Importing metadata") import_metadata(options) + if options.exportfile: + print("Exporting database") + export_database(options) if options.import_descriptions: print("Import descriptions") import_descriptions(options)