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 Pyqt 如何正确的关掉Qthread,并且释放其中的锁?
  • ¥30 网站服务器通过node.js部署了一个项目!前端访问失败
  • ¥15 WPS访问权限不足怎么解决
  • ¥15 java幂等控制问题
  • ¥15 海湾GST-DJ-N500
  • ¥15 氧化掩蔽层与注入条件关系
  • ¥15 Django DRF 如何反序列化得到Python对象类型数据
  • ¥15 多数据源与Hystrix的冲突
  • ¥15 如何在线硕士了解,广告太多,希望有真实接触过的人回答下?(标签-学习|关键词-在线硕士)
  • ¥15 zabbix6.4与frp如何进行联动