Tuesday, 30 August 2016

sql2csv converter python

This script converts .sql files to .csv files so they’re easily readable . It also has the ability to simply list tables from .sql file and can also list columns from the specified table .  so without further ado lets see some screenshots of the script in action .

  • TL;DR

We’ll change this

 sql2csv_python_virkid_5

into this

sql2csv_python_virkid_4

Cool eh ?

  • Screenshots

 

sql2csv_python_virkid

obligatory help screen

sql2csv_python_virkid_1

listing tables of the database

sql2csv_python_virkid_2

listing columns of the specified table  (courses in this case)

sql2csv_python_virkid_6

Dumped records from the the specified table

 

u should now have a file called Db_table.csv like

sql2csv_python_virkid_3

and it can be opened using any CSV file viewer .

  • Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#SQL to CSV converter ( sql2csv )
#./VIRkid
#Script is in Alpha stage so even i don't expect it to work 100% .sql files have tons of different formats ;_;
#fb.com/virkid36
#visit : skidventures.blogspot.com
#form more skiddy stuff
##########################
import re
import sys
import argparse
parser=argparse.ArgumentParser(prog="SQL2CSV",description="A Small Script To Extract Tables from SQL Files into CSV Files\n./VIRkid")
parser.add_argument("-d","--database",help="Target .SQL file (lists tables of Database)",metavar="",required=True)
parser.add_argument("-t","--table",help="Target TABLE from .SQL file (lists columns of table)",metavar="")
parser.add_argument("-x","--dump",help="Dump the target table to CSV",action='store_true')
args=parser.parse_args()
 
def espacer(namestr,lenstr,title,ta):
    print "\n"+namestr
    print "\n[%s]"%lenstr
    flen=30
    bar='+'+"-"*(flen-1)+'+'
    print bar
    for t in ta:
        cspace=flen-len(t)-2
        print '| '+t+cspace*' '+'|'
    print bar
 
def tbdict(db):
    tbldict={}
    tbls=re.findall('CREATE TABLE (.(.+?). ((?s).+?));',db)
    for each in tbls:
        clist=[]
        tbl=each[1]
        each=each[2]
        cols=each.split('\n',1000)
        total=len(cols)-1
 
        for i in xrange(1,total):
             
            column=cols[i].split('`',3)[1]
            if not column in clist:
                clist.append(column)
        tbldict[tbl]=clist
    return tbldict
 
db=open(args.database,'r').read()
try:
     
    if args.table and args.dump:
        tbn=args.table
 
 
        values=re.findall("INSERT INTO `%s` .+?VALUES(?s).*?\);"%tbn,db)
        values=[i.split(' VALUES',2)[1] for i in values]
 
    #   print len(values)
        x=''.join(values).strip()
        matc=re.findall("\((.*)\)",x)
        tables=tbdict(db)
        tables=tables[tbn]
        ccount=len(tables)
    #   print ccount   
        toprow=','.join(tables)+'\n'
        fmatc=[]
        for each in matc:
            if ", '" in each and not ", NULL" in each:         
                each=each.split(", '",ccount)
            elif ", NULL," in each:
                each=each.split(", ",ccount)
            for col in each:
                if ',' in col:
                    repn=each.index(col)
                    fent='"'+col+'"'
                    each.pop(repn)
                    each.insert(repn,fent)
 
            fmatc.append(', '.join(each).replace("'",''))
 
        f=open('%s'%args.database.split('.',2)[0]+'_'+tbn+'.csv','w')
        f.write(toprow)
 
        f.write('\n'.join(fmatc))
        f.close()
    #   print len(fmatc)   
        print "\n[+] Dumped %d  Records"%len(fmatc)
        sys.exit(0)
 
    if args.database and not args.table:
        tables=tbdict(db).keys()
        espacer('\n[+] Database: %s'%(args.database),'%d Tables'%len(tables),'Some title',tables)
        sys.exit(0)
    if args.table:
         
        columns=tbdict(db)[args.table]
        espacer('\n[+] Database: %s\n[+] Table : %s'%(args.database,args.table),'%d Columns'%len(columns),'Some title',columns)
        sys.exit(0)
except KeyboardInterrupt:
    "\n[-] Abort Signal Detected"
except Exception, e:
    print "\n[-] Unable to dump\n[-] %s"%str(e)

Related Posts:

  • MYSQL Db dump tool tbl-xtract Tbl-Xtract is a script to dump huge databases Quickly . It can dump 5 thousand records in one request (Quite fast huh ? ) . It also supports Post SQL… Read More
  • Symlink Bypass Script So today I'll share the first useful script i coded . It's a symlink script . unlike other symlink scripts this script doesn't need permission to r… Read More
  • sql2csv converter pythonThis script converts .sql files to .csv files so they’re easily readable . It also has the ability to simply list tables from .sql file and can also l… Read More
  • Kyuubi Reverse Admin Finder (Updated) Kyuubi is a Reverse Admin Finder . It finds all the domains on a specific host using yougetsignal and then tries to find the Admin Panels of the doma… Read More

1 comment:

  1. This post is so helpfull and informative.keep updating with more information...
    Angular Software
    Advantages Of Angular JS

    ReplyDelete