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)))
Wydruki:
insert into song_table (artist,title) values ('artist 1', 'song 1')
Psycopg adaptuje tuple
do record
i AsIs
robi to, co zostałoby zrobione przez podstawienie łańcuchów Pythona.