切片如何转数组?

2019-09-25 16:28:50 +08:00
 252748371

切片如何转数组? gorm 的 in 查询不支持切片

3850 次点击
所在节点    Go 编程语言
9 条回复
codehz
2019-09-25 16:33:39 +08:00
首先必须长度固定,然后
var target[5]int
copy(target[:], source)
252748371
2019-09-25 16:39:43 +08:00
@codehz
问题是长度不知道啊
codehz
2019-09-25 16:44:31 +08:00
那就
target := make([]int, n)
copy(target[:], source)
chotow
2019-09-25 16:50:39 +08:00
gorm 的 in 查询是支持切片的: http://gorm.io/docs/query.html#Plain-SQL
xorm 不支持(吐血)
kedadiannao220
2019-09-25 17:22:31 +08:00
252748371
2019-09-25 17:30:44 +08:00
@chotow
哥 帮我看看
sql: converting argument $4 type: unsupported type []string, a slice of string
252748371
2019-09-25 17:40:10 +08:00
解决了
原来不能传指针!
chotow
2019-09-27 16:39:08 +08:00
@kedadiannao220 #5
xorm Raw SQL 查询的时候,怎么实现 in 传值呢?
我试了下:db.SQL("select * from test").In("id", []uint64{1, 2, 3}).Find(&ret)
这样子并不可以
kedadiannao220
2019-11-15 17:24:12 +08:00
@chotow
使用 raw sql,就不用再使用 In、where 这些函数了;
db.SQL("select * from test where id in xxxx"),这样会存在 sql 注入的风险

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/604089

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX