Espero que les sea de utilidad, o que al menos sirva de ejemplo de programación en python =)
NOTA: Luego haber programado la herramienta, encontré en la misma página de sqlite que existe una interfaz de línea de comandos similar, pero el formato de la salida es bastante feo...
Tengan en cuenta que no me tomé ni un minuto en optimizar el script y que tampoco lo revisé demasiado... es decir, puede fallar =P
El script requiere un solo parámetro, la dirección del archivo que contiene la base de datos. Luego pueden ejecutar cualquier comando que SQLite les permita. Para salir, simplemente aprieten Ctrl+c
Ejemplo de uso:
#!/usr/bin/python
######################################################
# Created by: d3m4s1@d0v1v0
# Date: 2010-09-30
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License v2 as published by
# the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
######################################################
import sys
import os
import sqlite3
import readline
if(len(sys.argv)<2):
print "usage: "+sys.argv[0]+" <database-file>"
quit(-1)
sto_file=sys.argv[1]
print "SQLite command line shell by d3m4s1@d0v1v0"
if(not os.path.exists(sto_file)):
print "database file "+sto_file+" doesn't exists!\nexiting...."
quit(-1)
try:
sqlconn = sqlite3.connect(sto_file)
sqlconn.row_factory = sqlite3.Row
sqlcursor = sqlconn.cursor()
except Exception as detail:
print "something happend!"
print detail
quit(-2)
history = []
while (True):
try:
query = raw_input("> ")
result = sqlcursor.execute(query).fetchall()
c = 0
colsize = []
col_names = []
for col_name in sqlcursor.description:
col_names.append(str(col_name[0]))
colsize.append(len(str(col_name[0])))
for row in result:
c = 0
for cell in row:
if(len(str(cell))>colsize[c]):
colsize[c] = len(str(cell))
c += 1
hyphenrow = ''
for s in colsize:
hyphenrow += "+"+"-"*(s+2)
hyphenrow += "+\n"
c = 0
sys.stdout.write(hyphenrow)
for col in col_names:
sys.stdout.write("| "+col+" "*(colsize[c] - len(col))+" ")
c += 1
sys.stdout.write("|\n")
output = ""
for row in result:
c = 0
output += hyphenrow
for cell in row:
output += "| "+str(cell)+" "*(colsize[c] - len(str(cell)))+" "
c += 1
output += "|\n"
output += hyphenrow
print output
except sqlite3.OperationalError as detail:
print detail
pass
except KeyboardInterrupt:
print "exiting..."
sqlcursor.close()
quit(1)
$ sqlite.py crawled_db.dat
> select * from ip_domain LIMIT 3 OFFSET 1
+----------------+-----------------------+
| ip | domain |
+----------------+-----------------------+
| 207.200.74.91 | search.dmoz.org |
+----------------+-----------------------+
| 205.188.101.23 | blog.dmoz.org |
+----------------+-----------------------+
| 207.200.81.151 | report-abuse.dmoz.org |
+----------------+-----------------------+
0 comentarios:
Publicar un comentario