douyannuo7733 2016-05-09 21:10
浏览 79
已采纳

Go语言-在没有准备好的语句的情况下将数据插入Mysql数据库

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

  • 写回答

1条回答 默认 最新

  • dpd2349 2016-05-10 08:38
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真