I have 2 functions, one of which works and one of which doesn't. I do not understand this, because the only difference is that the second function puts in just the name of the variable, where the 1st function puts in the value.
There is only one line to build for the test from a given dataset, but the content function has several lines. I am using a for loop to handle the iteration through the csv to get content, but it fails to get anything but the first line. There is no error traceback from this code, unless I take the quotes from around the vuln_0 variable name cur.execute("INSERT INTO vulnerabilities(IP) VALUES ('vuln_0');")
With no quotes, it errors out in sqlite3, saying "there is no column vuln_0."
def load_titles(f, d):
print("This is da 'd,' y\'all! ", d )
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)
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]),
try:
con = lite.connect(d)
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, d):
filename = f
with open(filename, 'rb') as mycsv:
print(filename)
id = 1
reader = csv.reader(mycsv)
counter = 0
for counter,row in enumerate(reader):
# con = lite.connect(d)
if counter > 8:
continue
print(row)
# chine = (test.tests.id, row[0],row[2], row[4])
# vuln = (id, row[0],row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15], row[16], row[17])
vuln_0 = str(row[0])
vuln_1 = str(row[1])
vuln_2 = str(row[2])
vuln_3 = str(row[3])
vuln_4 = str(row[4])
vuln_5 = str(row[5])
vuln_6 = str(row[6])
vuln_7 = str(row[7])
vuln_8 = str(row[8])
vuln_9 = str(row[9])
vuln_A = str(row[10])
vuln_B = str(row[11])
vuln_C = str(row[12])
vuln_D = str(row[13])
vuln_E = str(row[14])
vuln_F = str(row[15])
vuln10 = str(row[16])
vuln11 = str(row[17])
try:
con = lite.connect(d)
with con:
cur = con.cursor()
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.execute("INSERT INTO vulnerabilities(IP) VALUES ('vuln_0');")
con.commit()
except lite.Error, e:
if con:
con.rollback()
print "Error %s:" % e.args[0]
sys.exit(1)
finally:
if con:
con.close()
#
There are 19 values I want to enter, but for now, only 0th value is being entered into the db.
The first iteration of the for loop runs, but nothing else.
A data file you could use: https://github.com/wolf29/csv-edit/blob/master/mouse.csv
The source: https://github.com/wolf29/csv-edit/blob/master/test0813.py
This is python 2.7.3. It has occurred to me that I might be mixing ver 2.x and 3.x in my code.