I have a csv that needs to be put into a database format so I can sort and count the data.
My approach has been to use python csv and sqlite modules.
I am communicating with the database and creating one of the 2 tables.
Second table has many more rows and I am wanting to use an append to db model which isn't working.
The code is
def load_titles(f):
filename = f
titles=[]
with open(filename, 'rb') as mycsv:
reader = csv.reader(mycsv)
counter = 0
for counter,row in enumerate(reader):
if counter < 1: continue
if counter > 6: break
titles.append(row)
# print titles
tests = (1, titles[0][0], titles[0][1], titles[0][2], titles[0][3], titles[0][4], titles[0][5], titles[0][6], titles[1][0], titles[1][1], titles[1][2], titles[4][0], titles[4][1], titles[4][2], titles[4][3], titles[4][4], titles[4][5], titles[4][6]),
# print(tests)
try:
con = lite.connect('test.db')
with con:
cur = con.cursor()
cur.execute("DROP TABLE IF EXISTS tests")
cur.execute("CREATE TABLE tests(Id INTEGER PRIMARY KEY, Corp TEXT, Address_1 TEXT, Address_2 TEXT, City TEXT, State TEXT, Country TEXT, Postal_Code TEXT, Requester TEXT, Code_1 TEXT, Role TEXT, Asset_Groups TEXT, IPs TEXT, Active_Hosts INT, Hosts_Matching_Filters INT, Trend_Analysis TEXT, Date_Range TEXT, Asset_Tags TEXT)")
cur.executemany("INSERT INTO tests VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", tests)
except lite.Error, e:
if con:
con.rollback()
print "Error %s:" % e.args[0]
sys.exit(1)
finally:
if con:
con.close()
def load_content(f):
filename = f
with open(filename, 'rb') as mycsv:
print(filename)
id = 1
reader = csv.reader(mycsv)
counter = 0
for counter,row in enumerate(reader):
if counter > 8:
continue
print(row)
# chine = (test.tests.id, row[0],row[2], row[4])
vuln = (id, str(row[0]),str(row[1]), str(row[2]), str(row[3]), str(row[4]), str(row[5]), str(row[6]), str(row[7]), str(row[8]), str(row[9]), str(row[10]), str(row[11]), str(row[12]), str(row[13]), str(row[14]), str(row[15]), str(row[16]), str(row[17]))
print "Look, this is vuln => ", vuln
try:
con = lite.connect('test.db')
with con:
cur = con.cursor()
# cur.execute("DROP TABLE IF EXISTS machines")
# cur.execute("CREATE TABLE machines(Id INTEGER PRIMARY KEY, Test_ID INT, IP TEXT, NetBIOS TEXT, OS TEXT)")
# cur.executemany("INSERT INTO machines VALUES(?, ?, ?, ?, ?)", chines)
# cur.execute("DROP TABLE IF EXISTS vulnerabilities")
cur.execute("CREATE TABLE vulnerabilities(Id INTEGER PRIMARY KEY, IP TEXT, CVSS_Base TEXT, NetBIOS TEXT, OS TEXT, QID TEXT, First_Detected TEXT, Last_Detected TEXT, Port TEXT, CVE_ID TEXT, Vendor_Reference TEXT, Bug_traq_ID TEXT, Threat TEXT, Impacts TEXT, Solution TEXT, Exploitability TEXT, Associated_Malware TEXT, Results TEXT, PCI_Vuln TEXT)")
cur.executemany("INSERT INTO vulnerabilities VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", vuln)
except lite.Error, e:
if con:
con.rollback()
print "Error %s:" % e.args[0]
sys.exit(1)
finally:
if con:
con.close()
and the error during the first run through the "for loop" is:
lin_content_test31.csv #print scaffolding tells me it is pulling the right file
['IP', 'CVSS Base', 'NetBIOS', 'OS', 'QID', 'First Detected', 'Last Detected', 'Port', 'CVE ID', 'Vendor Reference', 'Bugtraq ID', 'Threat', 'Impact', 'Solution', 'Exploitability', 'Associated Malware', 'Results', 'PCI Vuln']
Look, this is vuln => (1, 'IP', 'CVSS Base', 'NetBIOS', 'OS', 'QID', 'First Detected', 'Last Detected', 'Port', 'CVE ID', 'Vendor Reference', 'Bugtraq ID', 'Threat', 'Impact', 'Solution', 'Exploitability', 'Associated Malware', 'Results', 'PCI Vuln')
# Above is the scaffolding of the row and the captured values to be pushed into the db.
Traceback (most recent call last):
File "test0811.py", line 255, in <module>
main()
File "test0811.py", line 65, in main
lite_push_2 = load_content(outFileC)
File "test0811.py", line 242, in load_content
cur.executemany("INSERT INTO vulnerabilities VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", vuln)
ValueError: parameters are of unsupported type