-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert_withoutdbq.go
47 lines (36 loc) · 1.18 KB
/
insert_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
package main
import (
"context"
"database/sql"
"log"
"time"
_ "github.com/go-sql-driver/mysql"
)
func singleRowInsertWithoutDbq(ctx context.Context, db *sql.DB) {
// Without DBQ
newData := []interface{}{"400", "motor Cycle", 36, 446.46, 1, time.Now()}
insertQuery := "INSERT INTO store(id, product, quantity, price, available, timing) VALUES (?, ?, ?, ?, ?, ?)"
_, err := db.ExecContext(ctx, insertQuery, newData...)
if err != nil {
log.Fatal(err)
}
}
func multipleRowsInsertWithoutDbq(ctx context.Context, db *sql.DB) {
// multiple data to insert
newStores := [][]interface{}{
[]interface{}{"401", "Comic Book", "25", "456.34", "1", time.Now()},
[]interface{}{"402", "Movie Ticket", "17", "1250.5", "1", time.Now()},
[]interface{}{"403", "Teddy Bear", "30", "99.99", "0", time.Now()},
}
insertQuery := "INSERT INTO store(id, product, quantity, price, available, timing) VALUES (?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?)"
spreadData := []interface{}{}
for _, data := range newStores {
for _, col := range data {
spreadData = append(spreadData, col)
}
}
_, err := db.ExecContext(ctx, insertQuery, spreadData...)
if err != nil {
log.Fatal(err)
}
}