i want to insert the result from the below dictionary to the database ( i'm using PostgreSQL) :
companies_list = dict(zip(code, short_name))
the result for this dictionary after running the code and printing will show the following ( as an example ):
companies_list = { '00001': 'A', '00002': 'B', '0010': 'Z'}
so when i tried to insert the dictionary above to the database ( code ) below :
con = None
con = psycopg2.connect("dbname = 'testdb' user='user'")
cur = con.cursor()
cur.execute("CREATE TABLE companies_info(code TEXT PRIMARY KEY, short_name TEXT)")
query = "INSERT INTO companies_info ( code, short_name) VALUES ( %(code)s, %(short_name)s)"
cur.executemany(query, companies_list)
con.commit()
con.close()
i get the following:
cur.executemany(query, companies_list)
TypeError: string indices must be integers, not str
anyone can fix his error ?
code = ['00001', '00002', '0010']
short_name = ['A', 'B', 'Z']
companies_list = [tuple((t,)) for t in zip(code, short_name)]
query = "insert into companies_info (code, short_name) values %s"
cursor.executemany(query, companies_list)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.