douoyou3348 2017-06-19 21:12
浏览 355

可以从Golang中的sql.Rows获取SQL查询字符串吗?

I have the following code:

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)
}

The resulting rows structure is empty. I suppose it is because the interpolation of data is wrong. I am using PostgreSQL. Can I somehow get an SQL string from the rows instance?

  • 写回答

1条回答 默认 最新

  • douhuangjie4503 2017-06-19 21:17
    关注

    Can I somehow get an SQL string from the rows instance?

    No. This is easily answered by reading the documentation. Rows has no exported fields, and has only the following methods:

    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
    

    I have to wonder why you would want this... you already know what query was used.

    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答