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 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下