0
我正在使用postgres數據庫,並以用於API的字符串形式從數據庫返回Json響應。我的問題是,我不知道如何使用QueryRow方法檢查錯誤。這就是我的代碼,它正常工作。這只是表明Json的迴應。但是我有一個自定義函數,如果代碼出現錯誤,QueryRow不會讓我檢查錯誤是否有任何建議?Golang如何使用sql查詢行檢查錯誤
var result string
db.QueryRowContext(ctx,"select json_build_object('Profile', array_to_json(array_agg(t))) from" +
" (select p.id,p.fullname,z.thirtylatmin as latmin,z.thirtylatmax as latmax,z.thirtylonmin as lonmin," +
"z.thirtylonmax as lonmax,p.latitudes,p.longitudes,p.location as location" +
",p.picture,p.is_gold from profiles p join zips z on (z.city='Boston' " +
"and z.state='MA') where email=$1) t;",email).Scan(&result)
io.writestring(w, result)
如果我上面的代碼更改爲此然後它用錯誤的郵件,但是我怎麼能返回一個字符串
SqlStatement,err := db.QueryContext(ctx,"select json_build_object('Profile', array_to_json(array_agg(t))) from" +
" (select p.id,p.fullname,z.thirtylatmin as latmin,z.thirtylatmax as latmax,z.thirtylonmin as lonmin," +
"z.thirtylonmax as lonmax,p.latitudes,p.longitudes,p.location as location" +
",p.picture,p.is_gold from profiles p join zips z on (z.city='Boston' " +
"and z.state='MA') where email=$1) t;",email)
if err != nil {
log_errors("sql statement error",err.Error(),w)
}
io.WriteString(w, "How can I get SqlStatement as 1 whole String ??")
上面的語句,讓我記錄任何錯誤,並將其插入到數據庫,現在我怎麼能得到SqlStatement返回爲1整個字符串? SQL語句返回完美,現在我只需要讓它進入io.WriteString以字符串格式
完美非常感謝解決了這個問題 – user1591668