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)))
Drucke:
insert into song_table (artist,title) values ('artist 1', 'song 1')
Psycopg passt ein tuple
an zu einem record
und AsIs
macht das, was Pythons String-Ersetzung machen würde.