Trouble inserting data into Postgresql db via Python/psycopg2
Using a method (see bottom), I build an insert command to insert an item (stored as a dictionary) into my postgresql database. Although, when I pass that command to cur.execute I get a syntax error. I'm really at a loss as to why this error is occurring.
>>> print insert_string
"""INSERT INTO db_test (album, dj, datetime_scraped, artist, playdatetime, label, showblock, playid, showtitle, time, station, source_url, showgenre, songtitle, source_title) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);""", (item['album'], item['dj'], item['datetime_scraped'], item['artist'], item['playdatetime'], item['label'], item['showblock'], item['playid'], item['showtitle'], item['time'], item['station'], item['source_url'], item['showgenre'], item['songtitle'], item['source_title'])
>>> cur.execute(insert_string)
psycopg2.ProgrammingError: syntax error at or near """"INSERT INTO db_test (album, dj, datetime_scraped, artist, playdatetime, label, showblock, playid, showtitle, time, station, source_url, showgenre, songtitle, source_title) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);""""
LINE 1: """INSERT INTO db_test (album, dj, datetime_scraped, artis...
Here's a more "eyeball-friendly" version of that insert command:
"""INSERT INTO db_test (album, dj, datetime_scraped, artist, playdatetime, label, showblock, playid, showtitle, time, station, source_url, showgenre, songtitle, source_title)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);""",
(item['album'], item['dj'], item['datetime_scraped'], ite开发者_C百科m['artist'], item['playdatetime'], item['label'], item['showblock'], item['playid'], item['showtitle'], item['time'], item['station'], item['source_url'], item['showgenre'], item['songtitle'], item['source_title'])
The method used to build the insert:
def build_insert(self, table_name, item):
if len(item) == 0:
log.msg("Build_insert failed. Delivered item was empty.", level=log.ERROR)
return ''
#itemKeys = item.keys()
itemValues = []
for key in item.keys(): # Iterate through each key, surrounded by item[' '], seperated by comma
itemValues.append('item[\'{theKey}\']'.format(theKey=key))
sqlCommand = "\"\"\"INSERT INTO {table} ({keys}) VALUES ({value_symbols});\"\"\", ({values})".format(
table = table_name, #table to insert into, provided as method's argument
keys = ", ".join(item.keys()), #iterate through keys, seperated by comma
value_symbols = ", ".join("%s" for key in itemValues), #create a %s for each key
values = ", ".join(itemValues))
return sqlCommand
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
EDIT:
I used Gringo Suaves suggestions, with the exception of a small change to the build_insert method (create as many %s symbols as needed based on # of keys present.
def build_insert(self, table_name, item):
if len(item) == 0:
log.msg("Build_insert failed. Delivered item was empty.", level=log.ERROR)
return ''
keys = item.keys()
values = [ item[k] for k in keys] # make a list of each key
sqlCommand = 'INSERT INTO {table} ({keys}) VALUES ({value_symbols});'.format(
table = table_name, #table to insert into, provided as method's argument
keys = ", ".join(keys), #iterate through keys, seperated by comma
value_symbols = ", ".join("%s" for value in values) #create a %s for each key
)
return (sqlCommand, values)
Your string is not a valid SQL statement, it contains lots of python cruft.
I think I've fixed the method:
def build_insert(self, table_name, item):
if len(item) == 0:
log.msg('Build_insert failed. Delivered item was empty.', level=log.ERROR)
return ''
keys = item.keys()
values = [ item[k] for k in keys ]
sqlCommand = 'INSERT INTO {table} ({keys}) VALUES ({placeholders});'.format(
table = table_name,
keys = ', '.join(keys),
placeholders = ', '.join([ "'%s'" for v in values ]) # extra quotes may not be necessary
)
return (sqlCommand, values)
With some dummy data it returned the following tuple. I added a few newlines for clarity:
( "INSERT INTO thetable (album, dj, datetime_scraped, artist,
playdatetime, label, showblock, playid, songtitle, time, station,
source_url, showgenre, showtitle, source_title) VALUES ('%s', '%s',
'%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s',
'%s', '%s');",
['album_val', 'dj_val', 'datetime_scraped_val', 'artist_val',
'playdatetime_val', 'label_val', 'showblock_val', 'playid_val',
'songtitle_val', 'time_val', 'station_val', 'source_url_val',
'showgenre_val', 'showtitle_val', 'source_title_val']
)
Finally, pass it to cur.execute():
instr, data = build_insert(self, 'thetable', item)
cur.execute(instr, data)
You're missing '%' (before passing on parameters for the query ).
Basically you have to make sure that '%s' gets replaced by actual values .
For example : msg = 'world' Test = 'hello %s' % msg
'%' will replace the place holder with whatever is stored in the variable msg.
You can see in the error msg that psycopg is getting the query string with actual '%s' which is why it wouldn't run.
精彩评论