-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_withoutdbq.go
50 lines (38 loc) · 966 Bytes
/
query_withoutdbq.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"context"
"database/sql"
"fmt"
"log"
)
func singleRowQueryWithoutDbq(ctx context.Context, db *sql.DB, table string) interface{} {
res := &store{}
stmt := fmt.Sprintf("SELECT * FROM %s LIMIT 1", table)
err := db.QueryRowContext(ctx, stmt).Scan(&res.ID, &res.Product, &res.Price, &res.Quantity, &res.Available, &res.Timing)
if err != nil {
log.Fatal(err)
}
return res
}
func multipleRowsQueryWithoutDbq(ctx context.Context, db *sql.DB, table string) interface{} {
var results []interface{}
stmt := fmt.Sprintf("SELECT * FROM %s", table)
rows, err := db.QueryContext(ctx, stmt)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
res := &store{}
err := rows.Scan(&res.ID, &res.Product, &res.Price, &res.Quantity, &res.Available, &res.Timing)
if err != nil {
log.Fatal(err)
}
results = append(results, res)
}
err = rows.Err()
if err != nil {
log.Fatal(err)
}
return results
}