From 61f5add9c645f9efdbc56ff052cb2085d0cf639e Mon Sep 17 00:00:00 2001 From: john-g-g Date: Fri, 14 Feb 2014 14:37:21 -0500 Subject: [PATCH] Init --- .gitignore | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ lite2j.py | 49 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 .gitignore create mode 100755 lite2j.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..95142ef --- /dev/null +++ b/.gitignore @@ -0,0 +1,67 @@ +# OS X +.DS_Store +.DS_Store +.DS_Store? +._* +.Spotlight-V100 +.Trashes +ehthumbs.db +Thumbs.db + + +# Sqlite / Log +*.log +*.sql +*.sqlite + +# Packages +*.7z +*.dmg +*.gz +*.iso +*.jar +*.rar +*.tar +*.zip + +# Vim +[._]*.s[a-w][a-z] +[._]s[a-w][a-z] +*.un~ +Session.vim +.netrwhist +*~ + +# Python +*.py[cod] + +# C extensions +*.so + +# Packages +*.egg +*.egg-info +dist +build +eggs +parts +bin +var +sdist +develop-eggs +.installed.cfg +lib +lib64 +__pycache__ + +# Installer logs +pip-log.txt + +# Unit test / coverage reports +.coverage +.tox +nosetests.xml + +# Translations +*.mo +chat* diff --git a/lite2j.py b/lite2j.py new file mode 100755 index 0000000..3dd1907 --- /dev/null +++ b/lite2j.py @@ -0,0 +1,49 @@ +import sqlite3 +import sys +import json +import base64 + +def show_usage(): + print 'Usage:\n\tlite2j sqlitefile.db --exclude=table_name --exclude=table_name' + +exclude_tables = [] + +try: + for i, token in enumerate(sys.argv): + if i < 1: + continue + if i == 1: + db_file = token + if i > 1: + exclude_tables.append(token.split('--exclude=')[1]) + assert(db_file) +except (IndexError, NameError): + show_usage() + sys.exit(127) + +exclude_tables = set(exclude_tables) + +# Make sure blobs are base64 encoded +sqlite3.register_converter('BLOB', base64.b64encode) + +conn = sqlite3.connect(db_file, detect_types=sqlite3.PARSE_DECLTYPES) +cursor = conn.cursor() + +def get_table_list(cursor, exclude_tables=exclude_tables): + cursor.execute('SELECT * FROM main.sqlite_master WHERE type="table"') + return set(row[1] for row in cursor.fetchall()) - exclude_tables + +def get_column_names(cursor, table_name): + cursor.execute('SELECT * FROM %s' % table_name) + return [c[0] for c in cursor.description] + +def get_table(cursor, table_name): + column_names = get_column_names(cursor,table_name) + cursor.execute('SELECT * FROM main.%s' % table_name) + return [dict(zip(column_names, row)) for row in cursor.fetchall()] + +def get_tables(cursor): + table_list = get_table_list(cursor) + return [{table_name : get_table(cursor, table_name) for table_name in table_list}] + +print json.dumps(get_tables(cursor))