Does db.Query return ErrNoRows? - go

I can't seem to get ErrNoRows from my db.query when a sql query is expected to return no results.
results, err := database.Query("SELECT title FROM accountProject WHERE accountID=? AND MATCH(title) AGAINST(?)", accountID, query)
if err != nil {
if err == sql.ErrNoRows {
return errProjectDoesNotExist
}
return err
}

The value sql.ErrNoRows is not returned from DB.Query or DB.QueryContext. The first call to Rows.Next returns false when the result set has no rows.
The value sql.ErrNoRows is returned from a call to Row.Scan on a placeholder row returned from DB.QueryRow or DB.QueryRowContext. The DB.QueryRow* methods return a placeholder row on error instead of returning an error directly.
If the application is looking for zero or one rows, use QueryRow instead of Query.
row := database.QueryRow("SELECT title FROM accountProject WHERE accountID=? AND MATCH(title) AGAINST(?)", accountID, query)
err := row.Scan( .... address of variables here ... )
if err == sql.ErrNoRows {
return errProjectDoesNotExist
} else if err != nil {
return err
}

Related

How to select column specific in gorm?

I've tried to code for column specific select, but always fail with error messageScan error on column index 0, name "genre_name": unsupported Scan, storing driver.Value type string into type *models.MovieGenre;. How to solve it?
func (MovieRepositoryImpl *MovieRepositoryImpl) GetMovieById(id int) (*Movie, error) {
var movie Movie
err := MovieRepositoryImpl.DB.First(&movie, id).Error
if err != nil {
return nil, err
}
var movie_genres []*MovieGenre
err = MovieRepositoryImpl.DB.Select("genres.genre_name").
Joins("JOIN genres ON genres.id = movie_genres.genre_id").
Preload("Genre").Where("movie_id = ?", id).
Find(&movie_genres).Error
if err != nil {
return nil, err
}
movie.MovieGenre = append(movie.MovieGenre, movie_genres...)
return &movie, nil
}
without select the code is work, but I want to Select only genre name for API

Reading BigQuery in Golang. Not all expected results are given. What to do?

Given that the SQL is running perfectly in Query Editor. Still after assigning it to a struct, the data seems to have different values. Why is it like that?
var RunQuery = func(req *http.Request, query string)(*bigquery.RowIterator, error){
ctx := appengine.NewContext(req)
ctxWithDeadline, _ := context.WithTimeout(ctx, 30*time.Minute)
bqClient, bqErr := bigquery.NewClient(ctxWithDeadline, project, option.WithCredentialsFile(serviceAccount))
if bqErr != nil {
log.Errorf(ctx, "%v", bqErr)
return nil, bqErr
}
q := bqClient.Query(query)
job, err := q.Run(ctx)
if err != nil {
log.Errorf(ctx, "%v", err)
return nil, err
}
status, err := job.Wait(ctx)
if err != nil {
log.Errorf(ctx, "%v", err)
return nil, err
}
if err := status.Err(); err != nil {
log.Errorf(ctx, "%v", err)
return nil, err
}
it, err := job.Read(ctx)
if err != nil {
log.Errorf(ctx, "%v", err)
return nil, err
}
log.Infof(ctx, "Total Rows: %v", it.TotalRows)
return it, nil
}
type Customers struct {
CustomerName string `bigquery:"customer_name"`
CustomerAge int `bigquery:"customer_age"`
}
var rowsRead int
func main() {
query := `SELECT
name as customer_name,
age as customer_age
FROM customer_table
WHERE customerStatus = '0'`
customerInformation, customerInfoErr := RunQuery(req, query, false)
if customerInfoErr != nil {
log.Errorf(ctx, "Fetching customer information error :: %v", customerInfoErr)
return
}
for {
var row Customers
err := customerInformation.Next(&row)
log.Infof(ctx, "row %v", row)
if err == iterator.Done {
log.Infof(ctx, "ITERATION COMPLETE. Rows read %v", rowsRead)
break
}
rowsRead++
}
}
Let's say i have Query Results of
customer_name|customer_age
cat | 2
dog | 3
horse | 10
But after assigning it to a struct the results was
customer_name|customer_age
"" | 2
dog | ""
"" | ""
Why is it like this? i even tested it on chunk where i set the limit to 1000, still the same results. But the query results in Query Editor is what i expect
Solved it using Value Loader bigquery.Value. Instead of using expected struct in mapping the query results. used map[string]bigquery.Value. Still don't know why mapping query results with expected struct is not working perfectly. Here is my solution.
for {
row := make(map[string]bigquery.Value)
err := customerInformation.Next(&row)
log.Infof(ctx, "row %v", row)
if err == iterator.Done {
log.Infof(ctx, "ITERATION COMPLETE. Rows read %v", rowsRead)
break
}
rowsRead++
}
From the documentation:
If dst is a pointer to a struct, each column in the schema will be matched with an exported field of the struct that has the same name, ignoring the case. Unmatched schema columns and struct fields will be ignored.
cloud.google.com/go/bigquery
Here you try to resolve customer_age to a struct property named CustomerAge. If you update it to Customer_Age or customer_age it should work.

Calling rows.Err() in a multiple result set query with rows.NextResultSet()

I'm building a query with multiple result sets, following the example given, here. Important bits copied below for convenience.
My question is, should rows.Err() be called after each for rows.Next() {...} loop, rather than once at the very end as shown in the example? Why / why not? My understanding is that rows.Err() will catch errors, "... encountered during iteration", whereas rows.NextResultSet() will catch, "... [errors] advancing to [the next result set]". Seems like I would want to catch errors during each iteration, no? Thanks!
rows, err := db.Query(q, age)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
var (
id int64
name string
)
if err := rows.Scan(&id, &name); err != nil {
log.Fatal(err)
}
fmt.Printf("id %d name is %s\n", id, name)
}
if !rows.NextResultSet() {
log.Fatal("expected more result sets", rows.Err())
}
var roleMap = map[int64]string{
1: "user",
2: "admin",
3: "gopher",
}
for rows.Next() {
var (
id int64
role int64
)
if err := rows.Scan(&id, &role); err != nil {
log.Fatal(err)
}
fmt.Printf("id %d has role %s\n", id, roleMap[role])
}
if err := rows.Err(); err != nil {
log.Fatal(err)
}
You can find from source code of Next or Err or NextResultSet function that there is lasterr member of Rows structure which can be set by several ways and Err always return it if it isn't EOF.
Those ways are:
Next gets some error, sets lasterr and always returns false (so for rows.Next() {... doesn't iterate more and it is enough to check rows.Err() only after the loop.
NextResultSet also sets lasterr whenever it gets error and also returns false.
So answer is yes, it should. Because if NextResultSet after Next returns false its error replaces error from Next.

GO: Return map from SQL query

I am querying a mysql database in a GO function and want to return key value pairs in a map but can't quite figure out how to accomplish this. So far I have this function:
func GetData(callIds []string) map[string]Records {
//db insert
db, err := sql.Open("mysql", mySql)
if err != nil {
fmt.Printf(err.Error())
}
defer db.Close()
//db query
var foo string
err = db.QueryRow("select foo from bardata where callId = %v", 1).Scan(&foo)
if err != nil {
fmt.Printf(err.Error())
}
fmt.Println(foo)
return nil
I want to return a map with the key being callId and value being foo for each row returned from the query.
First, you need to build up your query. As it is, you're not even using your function input. Since we have a variable number of arguments, we need to do a little work to construct the right number of placeholders:
query := `select callid, foo from bardata where callid in (` +
strings.Repeat(`?,`, len(callIds) - 1) + `?)`
then, execute with the values passed in:
rows, err := db.Query(query, callIds...)
if err != nil {
// handle it
}
defer rows.Close()
then collect the results:
ret := map[string]string{}
for rows.Next() {
var callid, foo string
err = rows.Scan(&callid, &foo)
if err != nil {
// handle it
}
ret[callid] = foo
}
return ret
Caveats:
This will cause a placeholder mismatch error if callIds is an empty slice. If that's possible, then you need to detect it and handle it separately (maybe by returning an error or an empty map — querying the DB shouldn't be necessary).
This returns a map[string]string where the values are whatever "foo" is. In your question you have the function returning a map[string]Records but there's no information about what a Records might be or how to fetch one.
You might want to handle sql.ErrNoRows differently from other errors.
package main
import (
"fmt"
"github.com/bobby96333/goSqlHelper"
)
func main(){
fmt.Println("hello")
conn,err :=goSqlHelper.MysqlOpen("user:password#tcp(127.0.0.1:3306)/dbname")
checkErr(err)
row,err := conn.QueryRow("select * from table where col1 = ? and col2 = ?","123","abc")
checkErr(err)
if *row==nil {
fmt.Println("no found row")
}else{
fmt.Printf("%+v",row)
}
}
func checkErr(err error){
if err!=nil {
panic(err)
}
}
output:
&map[col1:abc col2:123]

How to get the count value from a go-sqlite3 query?

I am using go-sqlite3 to retrieve the number of rows with a column of a certain value:
query := "select count(notebook) from pages where notebook="
result, err := db.Query(fmt.Sprint(query, id))
Where id is passed to the function running the query.
How can I retrieve the count value from result?
This should work:
// Output will be stored here.
var output string
id := "1234"
// Prepare your query
query, err := db.Prepare("select count(notebook) from pages where notebook = ?")
if err != nil {
fmt.Printf("%s", err)
}
defer query.Close()
// Execute query using 'id' and place value into 'output'
err = query.QueryRow(id).Scan(&output)
// Catch errors
switch {
case err == sql.ErrNoRows:
fmt.Printf("No notebook with that ID.")
case err != nil:
fmt.Printf("%s", err)
default:
fmt.Printf("Counted %s notebooks\n", output)
}

Resources