我在 Golang 中使用“database/sql”模块,尝试执行如下查询:
select * from users
注意它没有任何参数。但是,我使用的方法会强制您在参数中包含参数:
db.Query(query string, args)
所以我不得不写这样的东西:
db.Query("select * from users where 1=?", 1)
运行无参数查询的方法是什么?
最佳答案
db.Query :
func (db *DB) Query(query string, args ...interface{}) (*Rows, error)
Query executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.
如果你真的使用database/sql
包,那么args
参数就是variadic :
The final incoming parameter in a function signature may have a type prefixed with .... A function with such a parameter is called variadic and may be invoked with zero or more arguments for that parameter.
所以,方法很简单:
db.Query("select * from users")
关于sql - 运行不带参数的 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56178312/