我有问题使用gorp更新postgresql数据库中的行,我成功地使用db.Exec运行更新,所有列都使用正确的信息更新,而gorp im只能更新非sql.Null*字段休息保持不变.
var db *sql.DB
var dbmap *gorp.DbMap
func getDB() (*sql.DB, *gorp.DbMap) {
if db == nil {
var err error
db, err = sql.Open("postgres", "postgres://xxxxxxxx")
db.SetMaxOpenConns(5)
db.SetMaxIdleConns(0)
dbmap = &gorp.DbMap{Db: db, Dialect: gorp.PostgresDialect{}}
dbmap.AddTableWithName(WirelessNetwork{}, "network").SetKeys(true, "Id")
if err != nil {
log.Panic(err)
}
}
return db, dbmap
}
type WirelessNetwork struct {
Id int `db:"id"`
Ssid string `db:"ssid"`
Lat sql.NullFloat64 `db:"lat"`
Lon sql.NullFloat64 `db:"lon"`
Sec sql.NullString `db:"sec"`
Bssid sql.NullString `db:"bssid"`
Channel sql.NullInt64 `db:"channel"`
Found bool `db:"found"`
Datefirst sql.NullString `db:"datefirst"` …Run Code Online (Sandbox Code Playgroud) 所以我不能太具体,但我认为我能告诉你的就足以解决这个问题了.首先,我正在使用gorp进行设置并获得交易.我正在使用github.com/denisenkom/go-mssqldb驱动程序.
然后我经历了一系列的操作,如果他们失败我rollback,如果一切都成功我commit.问题是它只会回滚失败的语句而不是其他操作.我错了,这不是假设工作的方式吗?
这是一些粗略的psudocode,让你更好地了解我在说什么:
trans,err := dbmap.Begin()
//assume all errors are received and checked before continuing
id := trans.Exec("insert thing") //successful, persists after rollback
thing := trans.Select("Select thing") //successful
trans.Exec("update other_thing with thing") //successful, persists after rollback
newthing := trans.Exec("insert new_thing with thing") //fails, rollsback
if err != nil{
trans.Rollback() //No errors
return
}
trans.Commit()
Run Code Online (Sandbox Code Playgroud)
我错了,那应该是rollback一切dbmap.Begin()吗?这是驱动程序实现中的错误吗?我们非常欢迎任何和所有的帮助.谢谢!
更新
测试了https://play.golang.org/p/0L3Vgk8C_F,它起作用,所以我猜这意味着它与gorp有关.我正在使用v1分支,因为这将很快生产,因此稳定性是关键.我将通过它,但看起来它只是轻轻地包裹它.
我已经rest api使用golang,gin和gorp
Employee structure:
type Employee struct {
Id int64 `db:"id" json:"id"`
Firstname string `db:"firstname" json:"firstname"`
Lastname string `db:"lastname" json:"lastname"`
Dob time.Time `db:"dob" json:"dob"`
Skills []string `db:skills json:"skills"`
}
Run Code Online (Sandbox Code Playgroud)
在POST发送请求时:
func PostEmployee(c *gin.Context) {
var emp Employee
c.Bind(&emp)
skills, _ := json.Marshal(emp.Skills)
if emp.Firstname != "" && emp.Lastname != "" {
if insert, _ := dbmap.Exec(`INSERT INTO employee (firstname, lastname, dob, skills) VALUES (?, ?, ?, ?)`, emp.Firstname, emp.Lastname, emp.Dob, skills); …Run Code Online (Sandbox Code Playgroud) 我正在尝试将 SQL 输出 (GORP) 转换为 JSON。我正在将gorp与 mySql 一起使用。
这是我选择的代码
type Mane struct {
ManeId string `db:"mane_id"`
Manetana string `db:"manetana"`
Yajamana string `db:"yajamana"`
}
var manegalu []Mane
_, err = dbmap.Select(&manegalu, "SELECT mane_id, manetana, yajamana FROM kd_mane")
//Option 1: Not working: Array of numbers. Not the actual json
a, err := json.Marshal(manegalu)
mt.Fprint(w, string(a))
//Option 2: Not working: Array of numbers. Not the actual json
for _, p := range manegalu {
a, err := json.Marshal(p)
fmt.Fprint(w, string(a))
} …Run Code Online (Sandbox Code Playgroud) 嗨,我正在使用gorp和mysql.当insert struct gorp返回时
reflect.Value.Interface:无法返回从未导出的字段或方法获得的值
在gorp docs中说Panics if any interface in the list has not been registered with AddTable但是我添加了这个结构
这个问题BUS结构有方法吗?我的节目和我的恐慌帮助了我
type BUS struct {
Id int64 `db:"Idx"`
Created int64
Writer string `db:"Writer"`
WriterId int64
Title string `db:"Title"`
Content string `db:"Content"`
Want int64
status int64
}
func (b BUS) search(bf Board_find) []BUS {
var arr []BUS
query, query_map := bf.Prepare()
_, err := dbmap.Select(&arr, query, query_map)
if err != nil {
log.Print(err)
}
return arr
}
func (b* BUS) write() …Run Code Online (Sandbox Code Playgroud) 问题1:
我有下面的MySQL查询工作正常但我刚刚发现这不是一个安全的方法,因为它开放的SQL注入.如果我想作为参数传递,你可以看到where子句是一个问题.
_, err := dbmap.Select(&response.AppsData, "SELECT...", ?)
Run Code Online (Sandbox Code Playgroud)
任何建议很多appriciated.
where := ""
for i := 0; i < (len(acl_user_apps)); i++ {
fmt.Println(acl_user_apps[i].AppId)
fmt.Println(acl_user_apps[i].Permissions)
if where == "" {
where = "WHERE Apps.id=" + strconv.Itoa(acl_user_apps[i].AppId)
} else {
where = where + " OR Apps.id=" + strconv.Itoa(acl_user_apps[i].AppId)
}
}
query := "SELECT Apps.*, GROUP_CONCAT(DISTINCT IFNULL(AppCategoryMatches.category_id,'-1') SEPARATOR ',') as temp, GROUP_CONCAT(DISTINCT IFNULL(AppCategories.category_name,'-1') SEPARATOR ',') as tmp_name FROM Apps LEFT JOIN AppCategoryMatches ON AppCategoryMatches.app_id=Apps.id LEFT JOIN AppCategories ON (AppCategoryMatches.`category_id` = AppCategories.id) " + …Run Code Online (Sandbox Code Playgroud)