You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am using goqu to create query and sqlx to execute using go-mssql . I am facing issue while inserting marshalled map to db , as it is storing value {\"AutomationAnalyticsUI\":\"heelo\",\"CheckAutoreview\":\"2.0\",\"MlDashboardToShow\":\"2.0\",\"OnboardingScreenShown\":false,\"rai\":\"pawan\"} in db which is creating error in unmarshalling.
type metadata struct {
ID int64 db:"id" json:"id" goqu:"skipinsert,skipupdate"
Metadata string db:"metadata_column" json:"metadata_colummn"
}
func main() {
dbAddress := "sqlserver://sa:Test12345@localhost:1433?database=lums"
//dbAddress := "root:test@(localhost:3306)/test?parseTime=true"
// Assuming db is your *sql.DB
db, err := sqlx.Connect("sqlserver", dbAddress) // Make sure to use the correct dialect
if err != nil {
fmt.Print(err.Error())
return
}
// Create an instance of your data
data := make(map[string]interface{}, 0)
data["AutomationAnalyticsUI"] = "heelo"
data["MlDashboardToShow"] = "2.0"
data["CheckAutoreview"] = "2.0"
data["OnboardingScreenShown"] = false
// Marshal your struct to a JSON string
jsonData, err := json.Marshal(data)
if err != nil {
fmt.Print(err.Error())
// handle error
return
}
updateJson := string(jsonData)
data1 := metadata{}
data1.Metadata = updateJson
// Convert jsonData (which is []byte) to string for insertion
dialect := goqu.Dialect("sqlserver")
//Use jsonString in your insert query
insertStatement := dialect.Insert("dummy").Rows(data1)
sql, _, err := insertStatement.ToSQL()
if err != nil {
fmt.Print(err.Error())
return
}
fmt.Println(sql)
// // Execute the insert statement
if _, err := db.Exec(sql); err != nil {
// handle error
fmt.Print(err.Error())
return
}
var getData metadata
getSQL := dialect.From("dummy").Select(getData)
sql, _, err = getSQL.ToSQL()
if err != nil {
fmt.Print(err.Error())
return
}
// // query the insert statement
if err := db.QueryRowx(sql).StructScan(&getData); err != nil {
// handle error
fmt.Println(err.Error())
return
}
metadataObj1 := make(map[string]interface{})
err = json.Unmarshal([]byte(getData.Metadata), &metadataObj1)
if err != nil {
fmt.Print(err.Error())
}
fmt.Println("------getData--------")
fmt.Println(getData)
fmt.Println("------metadata--------")
fmt.Println(metadataObj1)
}
`
Expected behavior
Query should look like this - INSERT INTO "dummy" ("metadata_column") VALUES ('{"AutomationAnalyticsUI":"heelo","CheckAutoreview":"2.0","MlDashboardToShow":"2.0","OnboardingScreenShown":false}')
Dialect:
postgres
mysql
sqlite3
[T] sqlserver
The text was updated successfully, but these errors were encountered:
pawan-lambda
changed the title
invalid character '\\' looking for beginning of object key
Incorrect query in sqlserver dialect
Mar 18, 2024
I am using goqu to create query and sqlx to execute using
go-mssql
. I am facing issue while inserting marshalled map to db , as it is storing value{\"AutomationAnalyticsUI\":\"heelo\",\"CheckAutoreview\":\"2.0\",\"MlDashboardToShow\":\"2.0\",\"OnboardingScreenShown\":false,\"rai\":\"pawan\"}
in db which is creating error in unmarshalling.To Reproduce
`package main
import (
"encoding/json"
"fmt"
)
type metadata struct {
ID int64
db:"id" json:"id" goqu:"skipinsert,skipupdate"
Metadata string
db:"metadata_column" json:"metadata_colummn"
}
func main() {
dbAddress := "sqlserver://sa:Test12345@localhost:1433?database=lums"
//dbAddress := "root:test@(localhost:3306)/test?parseTime=true"
// Assuming
db
is your *sql.DBdb, err := sqlx.Connect("sqlserver", dbAddress) // Make sure to use the correct dialect
if err != nil {
fmt.Print(err.Error())
return
}
}
`
Expected behavior
Query should look like this -
INSERT INTO "dummy" ("metadata_column") VALUES ('{"AutomationAnalyticsUI":"heelo","CheckAutoreview":"2.0","MlDashboardToShow":"2.0","OnboardingScreenShown":false}')
Dialect:
The text was updated successfully, but these errors were encountered: