Answer the question
In order to leave comments, you need to log in
Doesn't work f-I connect to the database with frequent calls.?
Good afternoon. There is a function for working with the database
func new_query_select(Query string, args ...interface{}) []mysql.Row{
db := mysql.New("tcp", "", "127.0.0.1:3306", "root", "password", "test")
err := db.Connect()
if err != nil {
log.Println("Не удалось подключиться к базе данных")
}
rows, _, err := db.Query(Query,args...)
if err != nil {
log.Println("Не удалось выполнить запрос",Query)
}
return rows
}
Answer the question
In order to leave comments, you need to log in
You are making too many connections.
You forget to close the resource obtained via db.Query(), of course, if you are using go-sql-driver/mysql and not something else.
You do not display the contents of err, but there can be a lot of useful things there, for example, the contents of an error ... in which it is written in simple language what exactly is broken.
Don't create a connection per request, create one connection (actually there's a connection pool inside) and just use it everywhere and don't forget to close resources.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question