Created
July 18, 2019 14:43
-
-
Save doxt3r/da5d288dd8210d5a8d8aa42cb7374add to your computer and use it in GitHub Desktop.
SQL to CSV converter efdefsefqdsfqsfsqdffqdsfsdqs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment