Yakirbu
Yakirbu

Reputation: 182

Go Language - Insert data to Mysql database without prepared statements

I have a script that needs to insert a huge amount of data to the database (270k rows) and I'm using prepared statements (with for loops). And when I execute (res, err := stmt.Exec) I can retrieve the last ID that was inserted to the database (id, err = res.LastInsertId()). But since I'm making a-lot of requests to the database, after 16k rows I get max_prepared_statements(16,382) error (which I then tried to set the max value to 1 million instead of 16,382 but the problem still remains).

My question is if there's another way to insert to DB and retrieve the last inserted ID without using prepared statements?

my insert code as for now is:

stmt, err := db.Prepare(`INSERT info SET title=?,minimage=?,downloadfile=?,rating=?,peoplewatched=?,likes=?`)
checkErr(err)
res, err := stmt.Exec(title,minimage,downloadfile,rating,peoplewatched,likes)
checkErr(err)
id, err  = res.LastInsertId()
checkErr(err)
fmt.Println(id)

Thanks

Upvotes: 0

Views: 986

Answers (1)

Yakirbu
Yakirbu

Reputation: 182

For those who encouter the same issue, make sure you call Prepare statement only once and leave it out of any of your loops. I just noticed that it was inside my 'for' loop and therefore it created a new prepare statment everytime.

Also thanks to Apin who mentioned it earlier

Upvotes: 2

Related Questions