2017-06-19 133 views
-1

我有以下代碼:是否有可能從Golang中的sql.Rows中獲取SQL查詢字符串?

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字符串嗎?

+1

這看起來很像[XY問題](http://xyproblem.info/)。你究竟在做什麼? – Flimzy

回答

1

我可以以某種方式從行實例中獲取SQL字符串嗎?

不,這很容易通過reading the documentation回答。行已經沒有出口領域,只有以下方法:

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 

我想知道你爲什麼會想這個...你已經知道使用的查詢。

相關問題