Golang how can I check for errors using the sql query row

huangapple go评论108阅读模式
英文:

Golang how can I check for errors using the sql query row

问题

我正在使用Postgres数据库,并从数据库返回一个字符串形式的Json响应,用于API。我的问题是,我不知道如何使用QueryRow方法检查错误。这是我的代码,它可以正常工作。它只是显示一个Json响应。然而,如果代码出现错误,我想使用一个自定义函数,但是QueryRow不允许我检查错误,有什么建议吗?

  1. var result string
  2. db.QueryRowContext(ctx, "select json_build_object('Profile', array_to_json(array_agg(t))) from" +
  3. " (select p.id,p.fullname,z.thirtylatmin as latmin,z.thirtylatmax as latmax,z.thirtylonmin as lonmin," +
  4. "z.thirtylonmax as lonmax,p.latitudes,p.longitudes,p.location as location" +
  5. ",p.picture,p.is_gold from profiles p join zips z on (z.city='Boston' " +
  6. "and z.state='MA') where email=$1) t;", email).Scan(&result)
  7. io.WriteString(w, result)

如果我将上面的代码更改为以下代码,则可以使用错误消息,但是如何将其作为字符串返回?

  1. SqlStatement, err := db.QueryContext(ctx, "select json_build_object('Profile', array_to_json(array_agg(t))) from" +
  2. " (select p.id,p.fullname,z.thirtylatmin as latmin,z.thirtylatmax as latmax,z.thirtylonmin as lonmin," +
  3. "z.thirtylonmax as lonmax,p.latitudes,p.longitudes,p.location as location" +
  4. ",p.picture,p.is_gold from profiles p join zips z on (z.city='Boston' " +
  5. "and z.state='MA') where email=$1) t;", email)
  6. if err != nil {
  7. log_errors("sql statement error", err.Error(), w)
  8. }
  9. io.WriteString(w, "如何将SqlStatement作为一个完整的字符串返回?")

上面的语句允许我记录任何错误并将其插入到数据库中,现在我需要将SqlStatement作为一个完整的字符串返回。SQL语句已经完美返回,现在我只需要以字符串格式将其传递给io.WriteString。

英文:

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 ?

  1. var result string
  2. db.QueryRowContext(ctx,"select json_build_object('Profile', array_to_json(array_agg(t))) from" +
  3. " (select p.id,p.fullname,z.thirtylatmin as latmin,z.thirtylatmax as latmax,z.thirtylonmin as lonmin," +
  4. "z.thirtylonmax as lonmax,p.latitudes,p.longitudes,p.location as location" +
  5. ",p.picture,p.is_gold from profiles p join zips z on (z.city='Boston' " +
  6. "and z.state='MA') where email=$1) t;",email).Scan(&result)
  7. 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

  1. SqlStatement,err := db.QueryContext(ctx,"select json_build_object('Profile', array_to_json(array_agg(t))) from" +
  2. " (select p.id,p.fullname,z.thirtylatmin as latmin,z.thirtylatmax as latmax,z.thirtylonmin as lonmin," +
  3. "z.thirtylonmax as lonmax,p.latitudes,p.longitudes,p.location as location" +
  4. ",p.picture,p.is_gold from profiles p join zips z on (z.city='Boston' " +
  5. "and z.state='MA') where email=$1) t;",email)
  6. if err != nil {
  7. log_errors("sql statement error",err.Error(),w)
  8. }
  9. 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

得分: 1

你应该检查Row.Scan方法的返回值,就像这样:

  1. row := db.QueryRow(
  2. `SELECT
  3. json_build_object('Profile', array_to_json(array_agg(t)))
  4. FROM
  5. (
  6. SELECT
  7. p.id,
  8. p.fullname,
  9. z.thirtylatmin as latmin,
  10. z.thirtylatmax as latmax,
  11. z.thirtylonmin as lonmin,
  12. z.thirtylonmax as lonmax,
  13. p.latitudes,
  14. p.longitudes,
  15. p.location as location,
  16. p.picture,
  17. p.is_gold
  18. FROM
  19. profiles p
  20. JOIN zips z on (z.city='Boston'AND z.state='MA')
  21. WHERE
  22. email=$1
  23. ) t`,
  24. email,
  25. )
  26. var result string
  27. if err := Scan(&result); err != nil {
  28. log.Fatal(err)
  29. }
  30. fmt.Fprint(w, result)
英文:

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

  1. row := db.QueryRow(
  2. `SELECT
  3. json_build_object('Profile', array_to_json(array_agg(t)))
  4. FROM
  5. (
  6. SELECT
  7. p.id,
  8. p.fullname,
  9. z.thirtylatmin as latmin,
  10. z.thirtylatmax as latmax,
  11. z.thirtylonmin as lonmin,
  12. z.thirtylonmax as lonmax,
  13. p.latitudes,
  14. p.longitudes,
  15. p.location as location,
  16. p.picture,
  17. p.is_gold
  18. FROM
  19. profiles p
  20. JOIN zips z on (z.city='Boston'AND z.state='MA')
  21. WHERE
  22. email=$1
  23. ) t`,
  24. email,
  25. )
  26. var result string
  27. if err := Scan(&result); err != nil {
  28. log.Fatal(err)
  29. }
  30. fmt.Fprint(w, result)

huangapple
  • 本文由 发表于 2017年8月3日 06:10:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/45471843.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定