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.

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

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP