亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

SQLite 行通過 shell 返回,但不在 Go 中

SQLite 行通過 shell 返回,但不在 Go 中

Go
米琪卡哇伊 2022-06-13 16:39:10
我有一個 SQLite 查詢,它在 shell 中返回預期的結果。但是,當我在 Go 程序中運行相同的查詢時,不會掃描任何值。這是我的查詢:sqlite> select html, text from messages where id="17128ab240e7526e";|Hey there在這種情況下,htmlisNULL并且text具有字符串"Hey there"。該表具有其他列和索引。這是我等效的 Go 代碼:package mainimport (    "database/sql"    "log"    _ "github.com/mattn/go-sqlite3")func main() {    filename := "emails.db"    conn, err := sql.Open("sqlite3", filename)    if err != nil {        log.Fatal(err)    }    row, err := conn.Query("select html, text from messages where id = ?", "17128ab240e7526e")    defer row.Close()    if err != nil {        log.Fatal(err)    }    hasRow := row.Next()    log.Println("Has row:", hasRow)    var html, text string    row.Scan(&html, &text)    log.Println("HTML:", html)    log.Println("TEXT:", text)}輸出是:$ go run main.go2020/07/05 21:10:14 Has row: true2020/07/05 21:10:14 HTML: 2020/07/05 21:10:14 TEXT: 有趣的是,這只發生在該列為html空時。如果html不為空,則無論text該列的值是否為空,都按預期返回數據。什么可以解釋這種行為?
查看完整描述

1 回答

?
慕的地10843

TA貢獻1785條經驗 獲得超8個贊

根據評論,我修改了使用的程序COALESCE并且工作正常。


關鍵是:不能scan NULL直接轉成字符串,Coalesce可以通過Query中的函數來克服這個問題。


row, err := conn.Query("select coalesce(html,'is-null'),text from messages where id =?", "17128ab240e7526e")

defer row.Close()

輸出:


arun@debian:stackoverflow$ go run main.go

2020/07/06 10:08:08 Has row: true

HTML: is-null

TEXT: Hey there


查看完整回答
反對 回復 2022-06-13
  • 1 回答
  • 0 關注
  • 130 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號