【问题标题】:Is it possible to get the SQL query string from sql.Rows?是否可以从 sql.Rows 获取 SQL 查询字符串?
【发布时间】:2017-11-22 05:29:46
【问题描述】:

我有以下代码:

selectPart := "id, user_id, date, time, minutes, details, created_at, updated_at, project_id"
sqlQuery := fmt.Sprintf("SELECT %s FROM time_entries WHERE user_id = $1 AND date >= $2 and date <= $3", selectPart)

var rows *sql.Rows
var err error

if project == nil {
    rows, err = DB.Query(sqlQuery, user.ID, formatDate(from), formatDate(to))
} else {
    rows, err = DB.Query(sqlQuery + " AND project_id = $4", user.ID, formatDate(from), formatDate(to), project.ID)
}

生成的行结构为空。我想这是因为数据的插值是错误的。我正在使用 PostgreSQL。我能以某种方式从rows 实例中获取 SQL 字符串吗?

【问题讨论】:

  • 这看起来很像XY Problem。你到底想做什么?

标签: sql postgresql go


【解决方案1】:

我可以从 rows 实例中获取 SQL 字符串吗?

没有。 reading the documentation 很容易回答这个问题。 Rows 没有导出字段,只有以下方法:

type Rows
    func (rs *Rows) Close() error
    func (rs *Rows) ColumnTypes() ([]*ColumnType, error)
    func (rs *Rows) Columns() ([]string, error)
    func (rs *Rows) Err() error
    func (rs *Rows) Next() bool
    func (rs *Rows) NextResultSet() bool
    func (rs *Rows) Scan(dest ...interface{}) error

我想知道你为什么想要这个......你已经知道使用了什么查询。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-06
    • 2011-09-10
    • 1970-01-01
    • 2012-07-27
    • 1970-01-01
    • 2015-02-26
    • 1970-01-01
    相关资源
    最近更新 更多