douzhang6176 2019-07-05 09:40
浏览 89
已采纳

有没有办法查看在sqlite中将参数应用于查询的结果?

I'm using sqlite3 in Go and for debugging purposes would like to see the result of applying arguments to the query with the replacements, i.e., with ? replaced by the actual argument. Is this possible?

Currently, I have code like the following and the FTS5 query printed looks alright:

SELECT ownerid FROM __ftsindex WHERE ownertable=? AND 
__ftsindex MATCH ? AND ( __ftsindex MATCH ? ) LIMIT ?;

It does not return an error, but it also doesn't find what it should find, and I'm at a loss how to further debug this:

for i := range trails {
    if i == 0 {
        s += `( __ftsindex MATCH ?`
    } else {
        s += ` OR __ftsindex MATCH ?`
    }
}
s += " )"
query = fmt.Sprintf(`SELECT ownerid FROM __ftsindex WHERE ownertable=? AND __ftsindex MATCH ? AND %s LIMIT ?;`, s)
fmt.Println(query)
args := make([]interface{}, len(trails)+3)
args[0] = cat.Table()
args[1] = searchTerm
for i := range trails {
    args[i+2] = trails[i]
}
args[len(args)-1] = limit
rows, err := model.DB.Base().Query(query, args...)
  • 写回答

1条回答 默认 最新

  • doutui7955 2019-07-05 15:41
    关注

    If you are not getting the results from an SQL query that you expect, there may be rogue bytes in your input arguments.

    I would suggest logging each individual argument using %+q:

    for i, arg := range sqlArgs {
        if _, ok := arg.(string); ok {
            log.Printf("arg %d) %+q
    ", i+1, arg) // catches any 'invisible' bytes or visually similar characters
        } else {
            log.Printf("arg %d) `%v`
    ", i+1, arg)
        }
    }
    

    For example: https://play.golang.org/p/mDsnIjMAVwP

    P.S. See this excellent go blog post on how to render obscure string encodings and byte-slices.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目