Hi,
I am a Newbie to GO language , working on Beacon(Bluetooth Low Energy) Receivers on aRaspberry Pi(Linux Board). Got to work to receive data from beacons , and my next task is to upload the same to azure sql server, which i am unable to insert the received variables into the sql statement . I am posting my code (sql code from azure samples) . Kindly provide the way to insert variables … recvid, recvtype, b.uuid, rssi . etc.
package main
import (
“fmt”
“log”
“context”
“strconv”
"github.com/paypal/gatt"
"github.com/paypal/gatt/examples/option"
_ "github.com/denisenkom/go-mssqldb"
"database/sql"
)
var db *sql.DB
var recvid string = “D1” – TO BE INSERTED INTO SQL
var recvtype string =“SLAVE” – TO BE INSERTED INTO SQL
func onPeripheralDiscovered(periphera gatt.Peripheral, advretisement *gatt.Advertisement, rssi int) {
b, err := NewiBeacon(advretisement.ManufacturerData)
if err == nil {
fmt.Println("UUID: ", b.uuid) — – TO BE INSERTED INTO SQL
fmt.Println("Major: ", b.major)
fmt.Println("Minor: ", b.minor)
fmt.Println("RSSI: ", rssi) — – TO BE INSERTED INTO SQL
rssival := strconv.Itoa(rssi)
fmt.Println("----RSSI STING: ", rssival)
insertvalues()
}}
func insertvalues(){
ctx := context.Background()
result, err := db.ExecContext(ctx,“INSERT INTO LOCATION_PAYLOAD (RSSI) VALUES (rssival)”) – NOT WORKING
if err != nil {
log.Fatal("Error creating connection pool: ", err.Error())
}
fmt.Println(“RESULT”,result)
}
func main() {
// Build connection string
connString := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%d;database=%s;",
server, user, password, port, database)
var err error
// Create connection pool
db, err = sql.Open("sqlserver", connString)
if err != nil {
log.Fatal("Error creating connection pool: ", err.Error())
}
ctx := context.Background()
err = db.PingContext(ctx)
if err != nil {
log.Fatal(err.Error())
}
fmt.Printf("Connected!\n")
}