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

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

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

问题

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

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, "如何将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 ?

 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

得分: 1

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

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)
英文:

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)

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:

确定