Using a Python dict for a SQL INSERT statement

前端 未结 10 1287
小蘑菇
小蘑菇 2020-11-28 22:43

I am trying to use a dict to do a SQL INSERT. The logic would basically be:

INSERT INTO table (dict.keys()) VALUES dict.values()
         


        
10条回答
  •  失恋的感觉
    2020-11-28 23:18

    You want to add parameter placeholders to the query. This might get you what you need:

    qmarks = ', '.join('?' * len(myDict))
    qry = "Insert Into Table (%s) Values (%s)" % (qmarks, qmarks)
    cursor.execute(qry, myDict.keys() + myDict.values())
    

提交回复
热议问题