sql - 运行不带参数的 SQL 查询

标签 sql go

我在 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/

相关文章:

javascript - Golang中如何使用javascript获取特定网站元素或提交表单

postgresql - 在 Postgresql 中存储和检索 Golang slice 、结构和结构 slice

go - 在 Golang 中将结构转换为映射的函数

string - panic : interface conversion: interface {} is **string, 不是字符串

go - 服务帐户流程-2条腿的Oauth Golang

sql - "Execute does not exist"写入 Postgres 函数时

sql - JOIN 的奇怪行为

sql - 即席 SQL 脚本中的编程 T-SQL

php - 如何在 UPDATE 语句中使用 SELECT 中的值?

mysql - 如何进行选择并能够回退到默认值?