Reputation: 51
I have a python list cve_id
, pkg_name
& vuln_status
. I'd like to import the data from these lists int a postgresql table
What I have tried:
from scanner import *
import psycopg2
try:
conn = psycopg2.connect(database="test", user="postgres",password="something", host="127.0.0.1", port="5432")
except:
print "Database un-successfull"
quit()
cur = conn.cursor()
cur.execute("INSERT into vuln_data(cve_id, pkg_name, status) VALUES (%s,%s, %s)", (cve_id, pkg_name, vuln_status))
conn.commit()
conn.close()
I get an error saying
psycopg2.DataError: multidimensional arrays must have array expressions with matching dimensions
^
Would love if someone could point out what can be done, here.
Thanks
Upvotes: 0
Views: 1893
Reputation: 11581
If your data is in the form:
[
(val, val, val),
(val, val, val),
(val, val, val),
]
Then the answer is cursor.executemany( sql, data ):
psycopg2: insert multiple rows with one query
If your data is not in this form, then put it in this form with zip() or list comprehensions.
There are HUGE performance implications.
Upvotes: 1
Reputation: 761
Try replacing the cur.execute
the code with the following
cur.execute("""INSERT into vuln_data(cve_id, pkg_name, status) VALUES (%s,%s, %s);""", (cve_id, pkg_name, vuln_status))
Make sure the items in the Lists are in a sequence, otherwise convert it to a tuple.
Upvotes: 0