I am using go-sql-driver/mysql to insert a ton of movies from the OMDB data dump. I am parsing the data and inserting it with the following code:
_, err = database.Query("INSERT INTO `movies` (`name`, `year`, `release_date`, `full_plot`, `genre`, `imdb_id`) VALUES (?, ?, ?, ?, ?, ?)", movie.Name, movie.Year, movie.ReleaseDate, movie.FullPlot, movie.Genre, movie.IMDBID)
if err != nil {
return false, nil
}
return true, nil
It works, but for only 150 rows. Am I doing something wrong?
Your code seems to discard the error value returned, which you shouldn't do; handle it gracefully. See what the error is, if you're opening too many connections to the DB, you should use a database connection pool and set the (*DB) SetMaxOpenConns
value.
(*DB)Query
is typically used for SELECT
statements that return rows, use (*DB)Exec
or (*Stmt)Exec
for your INSERT
.
I'd advise using a db connection pool, and (*sql.DB)Prepare
to prepare a statement and run your inserts (even concurrently) using the prepared statement.