Convert .sql to .csv
by Crazy driver - April 27, 2021 at 05:01 AM
#1
Hello guys, 

Anyone know how to convert sql file to csv? Please don't tell online websites, they are unable to convert big files... You guys know any software or script, please let me know. Thanks.
Reply
#2
python..................
Reply
#3
(April 27, 2021 at 06:28 AM)Weavers Wrote: python..................

You got some script? Most GitHub scripts are not working.
Reply
#4
https://anonfiles.com/j1L9tctau3/sql2csv_py

import re
import sys


def get_file_paths():
    #remove THIS filename from sys.argv if it's in there
    args = sys.argv
    if __file__.rstrip('.py') in args[0]:
        del args[0]
    try:
        input_file_path = args[0]
    except IndexError:
        print "Usage: sql2csv input_file [output_file]"
        sys.exit(1)
    try:
        output_file_path = args[1]
    except IndexError:
        output_file_path = "%s-sql2csv.csv" % input_file_path
    return input_file_path, output_file_path


def is_separator(line):
    """ Is the line just +-----+----+ ?"""
    return re.match("\+[+-]+\+", line)


def convert_line(line):
    #TODO: this could be better, as at the moment it removes all whitespace
    line = line.strip("|")
    line = line.replace("|", ",")
    line = line.replace(" ", "")
    return line


def convert_file(sql_file, csv_file):
    for line in sql_file.readlines():
        if is_separator(line):
            continue
        line = convert_line(line)
        csv_file.write(line)


def main():
    input_file_path, output_file_path = get_file_paths()
    sql_file = open(input_file_path, "r")
    csv_file = open(output_file_path, "w")
    convert_file(sql_file, csv_file)
    sql_file.close()
    csv_file.close()
    print "Done"
    sys.exit(0)

if __name__ == "__main__":
    main()


You're welcome Smile
Reply
#5
(April 27, 2021 at 09:37 AM)LulzSecurity Wrote: https://anonfiles.com/j1L9tctau3/sql2csv_py

import re
import sys


def get_file_paths():
    #remove THIS filename from sys.argv if it's in there
    args = sys.argv
    if __file__.rstrip('.py') in args[0]:
        del args[0]
    try:
        input_file_path = args[0]
    except IndexError:
        print "Usage: sql2csv input_file [output_file]"
        sys.exit(1)
    try:
        output_file_path = args[1]
    except IndexError:
        output_file_path = "%s-sql2csv.csv" % input_file_path
    return input_file_path, output_file_path


def is_separator(line):
    """ Is the line just +-----+----+ ?"""
    return re.match("\+[+-]+\+", line)


def convert_line(line):
    #TODO: this could be better, as at the moment it removes all whitespace
    line = line.strip("|")
    line = line.replace("|", ",")
    line = line.replace(" ", "")
    return line


def convert_file(sql_file, csv_file):
    for line in sql_file.readlines():
        if is_separator(line):
            continue
        line = convert_line(line)
        csv_file.write(line)


def main():
    input_file_path, output_file_path = get_file_paths()
    sql_file = open(input_file_path, "r")
    csv_file = open(output_file_path, "w")
    convert_file(sql_file, csv_file)
    sql_file.close()
    csv_file.close()
    print "Done"
    sys.exit(0)

if __name__ == "__main__":
    main()


You're welcome Smile


MemoryError, thanks btw.
Reply
#6
make sure you have the correct dependencies
try python3 instead
if you are on windows and those don't work try on a linux vm/vps/liveusb
Reply
#7
(April 27, 2021 at 01:08 PM)LulzSecurity Wrote: make sure you have the correct dependencies
try python3 instead
if you are on windows and those don't work try on a linux vm/vps/liveusb

I converted the script to python3 and tried, same error. I'll later try using python3---64bit. Thanks a lot btw.
Reply
#8
(April 27, 2021 at 01:20 PM)Crazy driver Wrote:
(April 27, 2021 at 01:08 PM)LulzSecurity Wrote: make sure you have the correct dependencies
try python3 instead
if you are on windows and those don't work try on a linux vm/vps/liveusb

I converted the script to python3 and tried, same error. I'll later try using python3---64bit. Thanks a lot btw.

No problem bro, I would have linked git if I had it saved but I don't remember which I got it from

just look up sql2csv github and I am sure you'll find it
Reply
#9
send me a file with the sample data. I can write code!
Reply
#10
Can you explain more specifically what file format you have?
Is ia SQL database file in * .mdf format?
Reply
#11
(April 27, 2021 at 09:37 AM)LulzSecurity Wrote: https://anonfiles.com/j1L9tctau3/sql2csv_py

import re
import sys


def get_file_paths():
    #remove THIS filename from sys.argv if it's in there
    args = sys.argv
    if __file__.rstrip('.py') in args[0]:
        del args[0]
    try:
        input_file_path = args[0]
    except IndexError:
        print "Usage: sql2csv input_file [output_file]"
        sys.exit(1)
    try:
        output_file_path = args[1]
    except IndexError:
        output_file_path = "%s-sql2csv.csv" % input_file_path
    return input_file_path, output_file_path


def is_separator(line):
    """ Is the line just +-----+----+ ?"""
    return re.match("\+[+-]+\+", line)


def convert_line(line):
    #TODO: this could be better, as at the moment it removes all whitespace
    line = line.strip("|")
    line = line.replace("|", ",")
    line = line.replace(" ", "")
    return line


def convert_file(sql_file, csv_file):
    for line in sql_file.readlines():
        if is_separator(line):
            continue
        line = convert_line(line)
        csv_file.write(line)


def main():
    input_file_path, output_file_path = get_file_paths()
    sql_file = open(input_file_path, "r")
    csv_file = open(output_file_path, "w")
    convert_file(sql_file, csv_file)
    sql_file.close()
    csv_file.close()
    print "Done"
    sys.exit(0)

if __name__ == "__main__":
    main()


You're welcome Smile

Lol why re-upload to anonfiles, it's on Github:

https://github.com/adamalton/sql2csv/blo...sql2csv.py

--

Download MySQL to your machine, then write a script to pull results from the database and output to CSV. Similar to here:
https://github.com/saianupkumarp/sql2csv

Trying to simply convert from a file only will be a fucking tough challenge whereas the above will likely be simple if you can use google.
Reply
#12
Why not use Python to parse it?
Reply

Possibly Related Threads…
Thread Author Replies Views Last Post
How TO Convert OST files into PST files & Office 365 edwinjarvis321 1 886 June 22, 2019 at 11:18 AM
Last Post: aaronfortin
SQL Scanner A tool that uses public proxies to find and scan sites for sql injection teamkelvinsecteam 0 815 January 04, 2019 at 10:39 PM
Last Post: teamkelvinsecteam
convertion mysql dump into csv itsmynew 11 2,726 August 04, 2017 at 03:34 PM
Last Post: samofstan

 Users browsing this thread: 1 Guest(s)