from psycopg2.extensions import AsIs
song = {
'title': 'song 1',
'artist': 'artist 1'
}
columns = song.keys()
values = [song[column] for column in columns]
insert_statement = 'insert into song_table (%s) values %s'
# cursor.execute(insert_statement, (AsIs(','.join(columns)), tuple(values)))
print cursor.mogrify(insert_statement, (AsIs(','.join(columns)), tuple(values)))
Tiskne:
insert into song_table (artist,title) values ('artist 1', 'song 1')
Psychopg přizpůsobuje tuple
do record
a AsIs
dělá to, co by udělala substituce řetězce v Pythonu.