I am getting an error while retrieving results from database I am having a global database variable which is serving for the whole project I have a test program to test the connection and retrieve a row while i am able to connect to the database but there is an error in retrieving the row Here is my code
//global database object for every package
var (
db *sql.DB
)
func initDatabase() bool {
var err error
db, err = sql.Open("mysql", "root:admin@/ipuscraper")
if err != nil {
fmt.Println("Error in database connection")
return false
}
defer db.Close()
err = db.Ping()
if err != nil {
fmt.Println("Error in database call")
return false
}
var str string
err = db.QueryRow("Select title, date, url from datesheet_ipu order by id DESC LIMIT 1").Scan(&str)
if err != nil {
fmt.Println("error in returning result")
return false
}
fmt.Println(str)
return true
}
The error is shown at db.Query() line and the error is not nil is there something wrong i am doing ?