4

I have following psql query to insert data to database

sql = ("INSERT INTO kart_user (custid,token,cycle,userid,proxyid,salesrepid,users,buyer,salesrep,validfrom,validto,discount,category,ratioOnly,proxy,notified) ""VALUES (%s, %s, %s, %s,%s, %s, %s, %s,%s, %s, %s, %s,%s, %s, %s, %s)")
result = self.cur.execute(sql,data)
self.dbconn.commit()
return result

Now, the problem I facing ,in some case data may contain multiple rows.in this case how can i rewrite my code. Note: I don't like to use for loop for data iteration ,please suggest better way to solve this issue.

alecxe
  • 441,113
  • 110
  • 1,021
  • 1,148
Abdul Razak
  • 2,404
  • 2
  • 16
  • 24
  • Maybe this is what you are looking for: [code](http://stackoverflow.com/questions/6889065/inserting-multiple-rows-in-mysql) Good luck! – giannis.t Jan 18 '16 at 11:38

1 Answers1

4

executemany() would help:

result = self.cur.executemany(sql, data)

data in this case should be a list of lists or a list of tuples.

alecxe
  • 441,113
  • 110
  • 1,021
  • 1,148