如何使用Prepare()?

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

How to use Prepare()?

问题

Prepare()的规范中,我以为我可以像这样使用Prepare()来执行一个带有sql查询的操作:

st, err := db.Prepare("SELECT name FROM pet WHERE name=?", "Fluffy")

但是我得到了这个错误:

# command-line-arguments
.\dbtest2.go:25: 调用db.Prepare时参数过多

这个是我能找到的唯一一个使用Prepare()的例子,但他没有使用带有参数的查询。我该如何使用Prepare()?

英文:

From the specs of Prepare() I thought I can use an sql query with Prepare() like this:

st, err := db.Prepare("SELECT name FROM pet WHERE name=?", "Fluffy")

But I get this error:

# command-line-arguments
.\dbtest2.go:25: too many arguments in call to db.Prepare

This is the only example I could find using Prepare() but he does not use queries with parameters. How do I use Prepare()?

答案1

得分: 2

看一下你链接的示例脚本的更下面部分,你会找到这个...

st, err := db.Prepare("INSERT INTO document (title) VALUES (?)")
if err != nil{
 fmt.Print( err );
 os.Exit(1)
}
st.Exec("Hello Again")
st.Close()
英文:

Look further down the example script that you linked to, and you find this...

st, err := db.Prepare("INSERT INTO document (title) VALUES (?)")
if err != nil{
 fmt.Print( err );
 os.Exit(1)
}
st.Exec("Hello Again")
st.Close()

huangapple
  • 本文由 发表于 2013年7月12日 04:47:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/17603204.html
匿名

发表评论

匿名网友

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

确定