4 Commits

Author SHA1 Message Date
82d43af480 fixed undefined exclude_tables 2016-05-07 03:45:28 +02:00
John G G
353a3dcf63 Update README.md 2014-03-04 14:59:13 -05:00
John G G
9822db5a4c Merge pull request #1 from sneak/installer
refactored script, created setuptools config
2014-02-16 19:04:56 -05:00
Jeffrey Paul
43d561f2f9 refactored script, created setuptools config 2014-02-17 00:46:14 +01:00
4 changed files with 95 additions and 50 deletions

View File

@@ -1,2 +1,20 @@
lite2j
sqlite2json
======
This script dumps all* the tables in an sqlite database as json.
## Usage
sqlite2json sqlitefile.db [--exclude=TABLE]
To dump the sqlite file where iMessages are stored:
sqlite2json ~/Library/Messages/chat.db
Here is a more useful version of the above example:
sqlite2json ~/Library/Messages/chat.db | jq -r .message[].text | grep -i loan
\*The script doesn't dump the contents of of the sqlite_master table

View File

@@ -1,49 +0,0 @@
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))

18
setup.py Normal file
View File

@@ -0,0 +1,18 @@
from setuptools import setup
setup(
name='sqlite2json',
version='1.0.0',
author='John gerlock',
author_email='john@pikkey.com',
packages=['sqlite2json'],
url='https://github.com/john-g-g/sqlite2json',
license=open('LICENSE').read(),
description='SQLite to JSON converter',
long_description=open('README.md').read(),
entry_points = {
'console_scripts': [
'sqlite2json = sqlite2json:main'
]
}
)

58
sqlite2json/__init__.py Executable file
View File

@@ -0,0 +1,58 @@
import sqlite3
import sys
import json
import base64
def main():
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)
print sqlite2json(
db_file=db_file,
exclude_tables=exclude_tables
)
def show_usage():
print 'Usage:\n\tsqlite2json sqlitefile.db --exclude=table_name --exclude=table_name'
def sqlite2json(exclude_tables=None,db_file=None):
# 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()
return json.dumps(get_tables(cursor))
def get_table_list(cursor, exclude_tables=set([]):
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}]
if __name__ == "__main__":
main()