1

I'm inserting data in a database with INSERT INTO

The main problem is that I'm inserting around 170k points of data and I'm looping a code like that:

for row in data:
   SQL='''INSERT INTO Table
       VALUES ({},{},{})
   '''.format(row[0],row[1],row[2])
   cur.execute(SQL)
cur.commit()
cur.close()
con.close()

This code is extremely slow, is there a faster way to do it?

I was thinking if there is a way to insert a whole column of my matrix data at once.

Lucas Tonon
  • 117
  • 8

2 Answers2

2

Try this. Basically you can achieve it using executemany() method.

import mysql.connector

mydb = mysql.connector.connect(
  .....
)
mycursor = mydb.cursor()

val = []
for row in data:
   val.append((row[0],row[1],row[2]))

sql = "INSERT INTO table (x,y,z) VALUES (%s, %s, %s)"

mycursor.executemany(sql, val)

mydb.commit()
apoorva kamath
  • 816
  • 1
  • 7
  • 19
1

Support may vary by DBMS (which you do not specify), but you can use a prepared statement, using your DBMS's paramstyle string in the VALUES clause, and pass a list of rows to the executemany() method. See the docs at https://www.python.org/dev/peps/pep-0249/#cursor-methods

rd_nielsen
  • 2,407
  • 2
  • 11
  • 18