#!/usr/bin/python

import sys
import getopt
import sqlite3


def dict_factory(cursor, row):
    d = {}
    for idx, col in enumerate(cursor.description):
        d[col[0]] = row[idx]
    return d


def db2json(file_in, file_out):
    connection = sqlite3.connect(file_in)
    connection.row_factory = dict_factory
    cursor = connection.cursor()
    cursor.execute("select id, time, "\
            "location_lat, location_lon from data")
    results = cursor.fetchall()
    print results
    print ""
    connection.close()


def print_usage():
    print 'db2json.py -i <inputfile.db> -o <outputfile.json>'


def print_version():
    print 'db2json v1.0 - sqlite3 database to json converter'


def process_args(argv):
    file_input = 'database.db'
    file_output = 'database.json'
    try:
        opts, args = getopt.getopt(argv,"vhi:o:",["ifile=","ofile="])
    except getopt.GetoptError:
        print_usage()
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print_usage()
            sys.exit()
        elif opt == '-v':
            print_version()
            sys.exit(0)
        elif opt in ("-i", "--ifile"):
            file_input = arg
        elif opt in ("-o", "--ofile"):
            file_output = arg
    return file_input, file_output


def main(argv):
    file_input, file_output = process_args(argv)
    db2json(file_input, file_output)


if __name__ == "__main__":
   main(sys.argv[1:])


@@ -54,16 +54,48 @@
 lite3%0A%0A%0A
+single_entry_per_node = False;%0A%0A
 def dict
@@ -380,33 +380,447 @@
     
-cursor.execute(%22select id
+if single_entry_per_node:%0A        print %22single entries%22%0A        cursor.execute(%0A        %22%22%22%0A        SELECT DISTINCT%0A        client_id_hash, time, location_lat, location_lon%0A        FROM data%0A        WHERE time IN%0A        (%0A        SELECT DISTINCT max(time)%0A        FROM    data%0A        GROUP BY%0A                client_id_hash%0A        );%0A        %22%22%22)%0A    else:%0A        print %22multiple entries%22%0A        cursor.execute(%22select client_id_hash
 , ti
@@ -826,16 +826,20 @@
 ime, %22%5C%0A
+    
         
@@ -1003,19 +1003,23 @@
 int 
-'db2json.py
+sys.argv%5B0%5D + '
  -i 
@@ -1055,16 +1055,204 @@
 .json%3E'%0A
+    print '  -v           print version'%0A    print '  -h           print help'%0A    print '  -s           include only one last entry per node in the'%5C%0A                         'JSON file'%0A
 %0A%0Adef pr
@@ -1376,19 +1376,8 @@
  = '
-database.db
 '%0A  
@@ -1464,16 +1464,17 @@
 ,%22vhi:o:
+s
 %22,%5B%22ifil
@@ -1738,16 +1738,124 @@
 exit(0)%0A
+        elif opt == '-s':%0A            global single_entry_per_node%0A            single_entry_per_node = True%0A
         
@@ -1979,24 +1979,105 @@
 utput = arg%0A
+%0A    if not file_input:%0A        print '%5BNote%5D No input data'%0A        sys.exit()%0A%0A
     return f

Add -s option to db2json