passing list values to an executemany cursor
Peter Tittmann <[email protected]>
| Newsgroups | gmane.comp.python.db.psycopg.devel |
|---|---|
| Message-ID | <[email protected]> |
Greetings,
I'm having trouble passing values (double precision) from a python list
to the executemany cursor. I am affraid this is a terribly simple issue
but it has stymied me completely, which reveals my very WBTE status with
regard to python. Here is my code:
import xlrd, psycopg2
con = psycopg2.connect("dbname=fb host=localhost port=5432 user=<user>
password=<password>")
bk = xlrd.open_workbook("<path>.xls")
cur=con.cursor()
#Named cells in an excel workbook....
frcs_names=['pmh_chainsaw','pmh_loaderb','pmh_loaders', 'pmh_truck']
frcs_o=[]
for j in frcs_names:
cv = bk.name_map[j]
for i in cv:
p=(i.cell().value)
frcs_o.append(p)
#make a dictionary for fun
fr_dict = dict(zip(frcs_names,frcs_o))
print fr_dict
cur.execute("DROP TABLE IF EXISTS pmh")
cur.execute("CREATE TABLE pmh ()")
for i in frcs_names:
cur.execute("ALTER TABLE pmh ADD COLUMN "+i+" double precision")
con.commit()
insert_st='INSERT INTO pmh VALUES ('+frcs_o+')'
cur.executemany(insert_st)
con.commit()
The result is as follows:
>>> {'pmh_chainsaw': 84.031250000000014, 'pmh_loaderb':
155.75707692307691, 'pmh_truck': 93.977430555555571, 'pmh_loaders':
126.94655384615383}
Traceback (most recent call last):
File "/tmp/py4195FHt", line 38, in <module>
insert_st='INSERT INTO pmh VALUES ('+frcs_o+')'
TypeError: cannot concatenate 'str' and 'list' objects
It appears that the connection is working and the lists have been
populated. I can check the db and see that the table has been created
with the appropriate column names. Any hints on correctly passing the
list falues from the 'frcs_o' list would be most gratefully recieved.
pt