从Go中的预处理语句和SELECT查询中检索值

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

Retrieving Values From a Prepared Statement and Select Query in Go

问题

我不确定如何从预处理语句中检索值,因为结果只返回有关事务的信息。

statement, err := txn.Prepare(`SELECT id, password FROM public.user WHERE email = $1`)
result, err = stmt.Exec(email, password, email)

我了解Query()和QueryRow()具有预期的结果,但据我了解,它们是不安全的。对此的任何帮助将不胜感激,谢谢。

英文:

I'm unsure how to retrieve values from a prepared statement since result only returns information about the transaction.

statement, err := txn.Prepare(`SELECT id, password FROM public.user WHERE email = $1`)
result, err = stmt.Exec(email, password, email)

I understand Query() and QueryRow() have the intended result, but from what I understand, they are unsafe. Any help on this is appreciated, thanks.

答案1

得分: 3

以下是翻译好的内容:

它们非常安全,使用方式相同,exec 实际上只是用于插入操作:

stmt, err := txn.Prepare(`SELECT id, password FROM public.user WHERE email = $1`)
rows, err := stmt.Query(email)

请查看:https://code.google.com/p/go-wiki/wiki/SQLInterface

英文:

They are perfectly safe, you use it the same way, exec is really just for inserts:

stmt, err := txn.Prepare(`SELECT id, password FROM public.user WHERE email = $1`)
rows, err := stmt.Query(email)

Check: https://code.google.com/p/go-wiki/wiki/SQLInterface

huangapple
  • 本文由 发表于 2014年8月6日 08:40:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/25150573.html
匿名

发表评论

匿名网友

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

确定