This commit is contained in:
john-g-g 2014-02-14 14:37:21 -05:00
commit 61f5add9c6
2 changed files with 116 additions and 0 deletions

67
.gitignore vendored Normal file
View File

@ -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*

49
lite2j.py Executable file
View File

@ -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))