Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
248 views
in Technique[技术] by (71.8m points)

sql - pyscopg2: Is it possible to dynamically add %s in loop

I am trying to create a method in python insert records into a table passing in a list of column names, and an associated list of records.

I was able to set it up where the column names populated dynamically via a for loop, but I can't figure out how to do the same thing with values because the psycopg2.executemany function relies on having %s's as placeholders.

Is it possible to have the number of %s's in the string populate dynamically via a loop? Is there another way to do this?

def load_table(dbname,table_name,fields,records):
        try:
                #Variable Qty Column Loop
                sql_fields = []
                for i in fields:
                        i = sql.Identifier(i)
                        sql_fields.append(i)

                #Need similar loop to replace %s values
                #Replace (%s,%s,%s) ???
                #.....
                #.....
                sql_values = []
                for i in fields:
                        sql_values.append('%s')

                print(sql_values)
                flist = sql.SQL(',').join(sql_fields)  
                connection, cursor = create_connection(dbname)
                insert_query = sql.SQL('INSERT INTO {table_name} ({fields}) VALUES (%s,%s,%s)').format(
                table_name = sql.Identifier(table_name),
                fields = flist,
                cursor.executemany(insert_query,records)
                print('Records Loaded Successfully')

        except (Exception,psycopg2.Error) as error:
                print("Failed to insert record into table {error}".format(error = error))

        finally:
                # closing database connection.
                if (connection):
                        close_connection(connection,cursor)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use sql.Placeholder, to populate the sql statement with the amount of %s-placeholders you need:

def load_table(dbname,table_name,fields,records):
        con, cur = create_connection('foo')
        query = sql.SQL("insert into {} ({}) values ({})").format(
            sql.Identifier(table_name),
            sql.SQL(', ').join(map(sql.Identifier, fields)),
            sql.SQL(', ').join(sql.Placeholder() * len(fields)))
        print(query.as_string(con))


if __name__ == '__main__':
    dbname = '...'
    table_name = 'messages'
    fields = ['user_id', 'message_type', 'message_title']
    records = [['12345', 'json', 'my first message'], ]

load_table(dbname,table_name,fields,records)

Output:

insert into "messages" ("user_id", "message_type", "message_title") values (%s, %s, %s)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...