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
  • 462,703
  • 120
  • 1,088
  • 1,195
Abdul Razak
  • 2,654
  • 2
  • 18
  • 24
  • Maybe this is what you are looking for: [code](http://stackoverflow.com/questions/6889065/inserting-multiple-rows-in-mysql) Good luck! – yannis.tz 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
  • 462,703
  • 120
  • 1,088
  • 1,195