Compare commits
9 Commits
feature-fo
...
master
Author | SHA1 | Date |
---|---|---|
Jeffrey Paul | 4031bbf751 | |
Jeffrey Paul | 26e4cc1063 | |
Jeffrey Paul | 6a56b2c3b4 | |
Jeffrey Paul | c280150707 | |
Jeffrey Paul | 359bf4feb7 | |
Jeffrey Paul | 028ab5910b | |
John G G | 353a3dcf63 | |
John G G | 9822db5a4c | |
Jeffrey Paul | 43d561f2f9 |
27
README.md
27
README.md
|
@ -1,2 +1,25 @@
|
||||||
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
|
||||||
|
|
||||||
|
# Authors
|
||||||
|
|
||||||
|
* John Gerlock `<john@pikkey.com>`
|
||||||
|
|
||||||
|
* Jeffrey Paul `<sneak@sneak.berlin>`
|
||||||
|
|
49
lite2j.py
49
lite2j.py
|
@ -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))
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
from setuptools import setup
|
||||||
|
|
||||||
|
setup(
|
||||||
|
name='sqlite2json',
|
||||||
|
version='1.0.1',
|
||||||
|
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'
|
||||||
|
]
|
||||||
|
}
|
||||||
|
)
|
|
@ -0,0 +1,61 @@
|
||||||
|
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)
|
||||||
|
output = {}
|
||||||
|
for table_name in table_list:
|
||||||
|
output[table_name] = get_table(cursor, table_name)
|
||||||
|
return output
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
Loading…
Reference in New Issue