drau89457 2017-08-02 22:10
浏览 88
已采纳

Golang如何使用sql查询行检查错误

I am using postgres database and am returning a Json response from the database in the form of a string which is used for an API . My problem is that I do not know how to check for errors using the QueryRow Method . This right here is my code and it works correctly. That just shows a Json response . However I have a custom function that I would like to use if there is some error with the code and QueryRow does not let me check for errors any suggestions ?

 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)

If I change the above code to this then it works with the error message however how can I return that in a String

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 ??")

The statement above lets me log any errors and insert it into the database, now how can I get SqlStatement to return as 1 whole string ? The SQL statement returns perfectly now I just need to get it into io.WriteString in a String format

  • 写回答

1条回答 默认 最新

  • doutan3192 2017-08-02 22:37
    关注

    You should just check return value of Row.Scan method, just like this:

    row := db.QueryRow(
        `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,
    )
    
    var result string
    if err := Scan(&result); err != nil {
        log.Fatal(err)
    }
    
    fmt.Fprint(w, result)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵