0

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

3
  • 1
    You should show more of your code. Most likely you never close your statements and/or rows. Commented May 9, 2016 at 21:44
  • 1
    Do you create new statement on each insert (calling db.Prepare)? Btw, you have another stackoverflow thread with the same question I think. Commented May 9, 2016 at 23:28
  • 1
    Possible duplicate of Cannot prepare more than max_prepared_stmt_count Mysql error Commented May 10, 2016 at 0:20

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

that why you need to attach more code here for better understand for others.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.