How can i insert value to my sql table

Hi All
when i run this function all the values are null .I have attach my code. Could you please some one correct it
import (
“log”
“io/ioutil”
“go/scanner”
“os”
“bufio”
“strings”
“strconv”
“fmt”
“time”
“database/sql”
_ “github.com/go-sql-driver/mysql
)
var db *sql.DB

func init() {
var err error
db,err = sql.Open(“mysql”,“root1:root1@tcp(localhost:3306)/lgforusage?charset=utf8”)
if err != nil{
panic(err)
}
if err = db.Ping(); err != nil {
panic(err)
}
fmt.Println(“You connected to your database”)

}

func ReadFiles(locationPath string) (string,error) {
var lines string

folder, err := ioutil.ReadDir(locationPath)
if err != nil {
log.Fatal(err)
//return nil,err

}

for _, f := range folder {
file, err := os.Open(locationPath+f.Name())
//lines ,err := ioutil.ReadFile(locationPath+f.Name())
if err != nil {
log.Fatal(err)
//return nil,err
}

var sb strings.Builder
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line:= scanner.Text()
sb.WriteString(line)
sb.WriteString(",")

}
lines = strings.TrimSuffix(sb.String(),",")
linejoin := strings.Join(strings.Fields(lines),"")
readline(linejoin)

if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}

return lines,scanner.Error{}
}
func readline( liness string ) {
s :=strings.Split(liness,",")
locationname,mac,forgusage,loadingtime,fgusage,enusage,ossidusage,boingousage,landlineusage,uptime,restrouter:=s[0],s[1],s[2],s[3],s[4],s[5],s[6],s[7],s[8],s[9],s[10]

fgusagesplit := strings.Split(forgusage,“MB”)
fgusagesplitvaltest :=fgusagesplit[0]

fguintval,err := strconv.ParseInt(fgusagesplitvaltest, 10, 64)
if err != nil{
fmt.Println(“ForGUsage not compatibe null”)
}
fmt.Printf("%T\n", fguintval)
//LoadingTime1,err := time.Parse(“Jan-02-2006”,LoadingTime)
//if err != nil{
// //panic(err)
// fmt.Println(“Time Convert Error”)
//}
fgusageval,err := strconv.ParseInt(fgusage,10,32)
if err != nil{
fmt.Println(“FourGUsage cannot be null”)
}
enusageval,err := strconv.ParseInt(enusage,10,32)
if err !=nil{
fmt.Println(“Ethanet useage cannot be null”)
}
ossidusageval,err := strconv.ParseInt(ossidusage,10,32)
if err != nil{
fmt.Println(“OOP Meadia value cannot be null”)
}
boingousageval,err := strconv.ParseInt(boingousage,10,32)
if err != nil{
fmt.Println(“BoingoUsage value cannot be null”)
}

landlineusageval,err := strconv.ParseInt(landlineusage,10,32)
if err != nil{
fmt.Println(“LandLineUsage value cannot be null”)
}
restrouterval,err :=strconv.ParseInt(restrouter,10,32)
if err != nil{
fmt.Println(“reset value not display”)
}
dt:= time.Now()
systemtime:= dt.Format(“01-02-2006 15:04:05”)
//UpTimeVal,err := time.Parse(“2006-06-02”,UpTime)
//if err != nil{
// fmt.Println(“UpTime value cannot bernull”)
//}

fmt.Println("LocationName = ",locationname)
fmt.Println(“Mac =”,mac)
fmt.Println(“FourGUsage =”,fguintval)
fmt.Println(“LoadingTime =”,loadingtime)
fmt.Println(“FGUsage =”,fgusageval)
fmt.Println(“ENUsage =”,enusageval)
fmt.Println(“OSSIDUsage =”,ossidusageval)
fmt.Println("BoingoUsage = ",boingousageval)
fmt.Println(“LandLineUsage =”,landlineusageval)
fmt.Println(“UpTime =”,uptime)
fmt.Println(“restrouter”,restrouterval)
fmt.Println(systemtime)

_,err =db.Exec(“insert into forgtb(locationname,mac,fgintval,loadingtime,fgusageval,enusageval,ossidusageval,boingousageval,landlineusageval,uptime,resetrouterval,systemtime)values (’$1’,’$2’,fgintval,’$4’,fgusageval,enusageval,ossidusageval,boingousageval,landlineusageval,’$10’,resetrouterval,systemtime)”)
if err!= nil{
panic(err)
fmt.Println(“insert query not working”,err.Error())
}

}

func main() {
var locationPath =“E:/”

ReadFiles(locationPath)

}

my Db output is
| locationname | mac | fgintval | loadingtime | fgusageval | enusageval | ossidusageval | boingousageval | landlineusageval | uptime | resetrouterval | systemtime |
±-------------±-----±---------±------------±-----------±-----------±--------------±---------------±-----------------±-------±---------------±-----------+
| $1 | $2 | NULL | $4 | NULL | NULL | NULL | NULL | NULL | $10 | NULL | NULL

1 Like

@sudesh can you wrap your code in three back ticks or post it into the Go Playground?

Here is a link talking about the use of code blocks; it works the same way here with the three back ticks: https://help.github.com/en/articles/creating-and-highlighting-code-blocks

1 Like

It would be better if you provide reproducible sample.
For now I only can recommend read this article:

Inserting records into a PostgreSQL database with Go’s database/sql package

and Go database/sql tutorial

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.