2

i try insert data with golang and gorm mysql driver but inserted value is null

var ipsin = ips{value: ipstr, org: org, country: country, city: city, latitude: latitude, longitude: longitude, isp: isp, asn: asn}

fmt.Println(ipsin)
//  result := db.Select("value", "org", "country", "city", "latitude", "longitude", "isp", "asn").Create(&ipsin)
result := db.Table("ips").Create(&ipsin)

thats my code please help me

1
  • 5
    You must export struct fields, start their names with capital letter, e.g. value => Value. Commented Oct 18, 2022 at 8:29

1 Answer 1

1

I think all data types are string and set mental primary key and etc. pls change it with your requirement.

you should do like below:

your DTO like it

type Ips struct {
     CreatedAt    time.Time  `gorm:"autoCreateTime"` //optional
     UpdatedAt    time.Time  `gorm:"autoUpdateTime"` //optional
     Value        string     `json:"value" binding:"required"`
     Org          string     `json:"org" gorm:"primaryKey"`
     Country      string     `json:"country"`
     City         string     `json:"city"`
     Latitude     string     `json:"latitude"`
     Longitude    string     `json:"longitude"`
     Isp          string     `json:"isp"`
     Asn          string     `json:"asn"`
}

create or change your table in DB

err := Client.AutoMigrate(&Ips{})

insert your data into table

result := Client.Create(&Ips{Value: ipstr, Org: org, Country: country, 
                        City: city, Latitude: latitude,        
                        Longitude: longitude, Isp: isp, Asn: asn})
Sign up to request clarification or add additional context in comments.

8 Comments

tested , not worked
Can i see ur code? I mean entire code
With new changes pls
type ips struct { value string json:"value" binding:"required" org string json:"org" country string json:"country" city string json:"city" latitude float64 json:"latitude" longitude float64 json:"longitude" isp string json:"isp" asn string json:"asn" created_at time.Time gorm:"autoCreateTime" //optional updated_at time.Time gorm:"autoUpdateTime" //optional }
var ipsin = ips{value: ipstr, org: org, country: country, city: city, latitude: latitude, longitude: longitude, isp: isp, asn: asn} result := db.Table("ips").Create(&ipsin) /*fmt.Println(ipsin.id) fmt.Println(result.Error) fmt.Println(result.RowsAffected)*/ //result := db.Raw("INSERT INTO ips( value, org, country, city,latitude,longitude, isp, asn) VALUES (?,?,?,?,?,?,?,?)").Scan(ipsin) resid := ips{value: ipstr} db.Save(&resid) if result.Error != nil { return false, 0 } else { return true, 1 }
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.