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

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

sql:列索引 19 上的掃描錯誤,名稱“L2Name”:不支持的掃描

sql:列索引 19 上的掃描錯誤,名稱“L2Name”:不支持的掃描

Go
拉丁的傳說 2023-06-05 18:21:47
使用 Golang 和內置的數據庫/sql 庫和 postgres lib/pq 庫,我試圖從一個數據庫中讀取一些記錄中有一些空值。代碼可以編譯,但是當我嘗試運行它時出現以下錯誤: sql: Scan error on column index 19, name "L2Name": unsupported Scan, storing driver.Value type <nil> into type *string我有這樣的結構:// Assets Info Datasettype AssetInfo struct {  Asset_id string  Asset_name string  Organisation_id string  LastCheckIn string  Asset_Status string  Asset_latitude string  Asset_longitude string  Organisation_name string  CurrentDevice_name string  AssetActiveDeviceType string  AssetSafetyTimer float32  TemplateName string  TemplateL2name string  TemplateL2contact string  TemplateL3name string  TemplateL3contact string  TemplateL4name string  TemplateL4contact string  TemplateEscalationNotes string}代碼的輸出沒問題,我可以從數據庫中獲取我想要的所有數據。除了 SQL 錯誤也打印在控制臺上。并且 L2Name 在這里不是空值,我可以在控制臺上打印該值。所以不知道為什么顯示類型錯誤?
查看完整描述

2 回答

?
白衣非少年

TA貢獻1155條經驗 獲得超0個贊

最簡單的修復方法是使用 COALESCE(templates.L2Name, '') 將可為空的列包裝在您的 sql 語句中,


? sqlstatement := "SELECT" +

? ? " assets.ID, assets.Name, assets.LastCheckIn, assets.Status, assets.OffTimer," +

? ? " assets.SafetyTimer, assets.HazardTimer, assets.HazardTimerStartedTime, assets.LastSignedOn," +

? ? " assets.Latitude, assets.Longitude, assets.TemplateID, assets.ActiveDeviceType, assets.CurrentDeviceID," +

? ? " assets.OffTimerTemp, assets.OrganisationID," +

? ? " organisations.Name As OrganisationName," +

? ? " devices.Label As CurrentDeviceName," +

? ? " templates.Name As TemplateName, COALESCE(templates.L2Name, ''), COALESCE(templates.L2Contact, '')," +

? ? " COALESCE(templates.L3Name, ''), COALESCE(templates.L3Contact, '')," +

? ? " COALESCE(templates.L4Name, ''), COALESCE(templates.L4Contact, ''), templates.Note" +

? ? " FROM assets" +

? ? " LEFT JOIN organisations ON assets.OrganisationID = organisations.ID" +

? ? " LEFT JOIN devices ON assets.CurrentDeviceID = devices.ID" +

? ? " JOIN templates ON assets.TemplateID = templates.ID" +

? ? " WHERE assets.Status != 'Not monitoring' AND assets.AssetStatus = 'Active' AND assets.Display != '0'"



查看完整回答
反對 回復 2023-06-05
?
守候你守候我

TA貢獻1802條經驗 獲得超10個贊

對于可以為 NULL 的字段,您應該使用 sql.NullString 數據結構。


查看完整回答
反對 回復 2023-06-05
  • 2 回答
  • 0 關注
  • 195 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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