Compare commits

...

9 Commits

Author SHA1 Message Date
Jeffrey Paul 4031bbf751
Merge branch 'master' of github.com:john-g-g/sqlite2json
Signed-off-by: Jeffrey Paul <jp@eeqj.com>
2016-05-07 04:08:09 +02:00
Jeffrey Paul 26e4cc1063 version bump to 1.0.1, added me to authors 2016-05-07 04:07:21 +02:00
Jeffrey Paul 6a56b2c3b4 actually fixes #4 now 2016-05-07 04:05:52 +02:00
Jeffrey Paul c280150707 actually fixes #4 2016-05-07 03:59:16 +02:00
Jeffrey Paul 359bf4feb7 fixes #4 2016-05-07 03:53:27 +02:00
Jeffrey Paul 028ab5910b fixes #2 2016-05-07 03:52:03 +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 104 additions and 51 deletions

View File

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

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.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'
]
}
)

61
sqlite2json/__init__.py Executable file
View File

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