英文:
Passing Variable to GoLang Query
问题
首先,让我说一下,我是一个初学者(几天前开始)使用golang,并且正在学习如何实际应用这门语言。我的目标是构建一个Web Rest API,该API查询数据库并将数据返回给用户。
我已经成功地使用martini(https://github.com/go-martini/martini)创建了一个简单的API,并使用https://github.com/go-sql-driver/mysql连接到了MySQL数据库。我目前的问题是如何将API请求中的变量参数传递到我的查询中。以下是我的当前代码:
package main
import (
"github.com/go-martini/martini"
_ "github.com/go-sql-driver/mysql"
"database/sql"
"fmt"
)
func main() {
db, err := sql.Open("mysql", "root:root@tcp(localhost:8889)/test")
m := martini.Classic()
var str string
m.Get("/hello/:user", func(params martini.Params) string {
var userId = params["user"]
err = db.QueryRow("select name from users where id = ?", userId).Scan(&str)
if err != nil && err != sql.ErrNoRows {
fmt.Println(err)
}
return "Hello " + str
})
m.Run()
defer db.Close()
}
如你所见,我的目标是将输入变量(user)插入到一个名为userId的变量中。然后,我想针对该变量查询数据库以获取姓名。一旦所有这些都正常工作,我想回复用户的姓名。
有人可以帮忙吗?非常感谢,我将继续学习Go!
英文:
first off let me say I'm a beginner (started a few days ago) with golang and am in the process of learning how to practically apply the language. My goal is to build a web Rest API that queries a database and provides data back to the user.
I've been able to successfully create a simple API using martini (https://github.com/go-martini/martini) and connect to a MySQL database using https://github.com/go-sql-driver/mysql. My problem at the current moment is how to pass a variable param from the API request into my query. Here is my current code:
package main
import (
"github.com/go-martini/martini"
_ "github.com/go-sql-driver/mysql"
"database/sql"
"fmt"
)
func main() {
db, err := sql.Open("mysql",
"root:root@tcp(localhost:8889)/test")
m := martini.Classic()
var str string
m.Get("/hello/:user", func(params martini.Params) string {
var userId = params["user"]
err = db.QueryRow(
"select name from users where id = userId").Scan(&str)
if err != nil && err != sql.ErrNoRows {
fmt.Println(err)
}
return "Hello " + str
})
m.Run()
defer db.Close()
}
As you can see my goal is to take the input variable (user) and insert that into a variable called userId. Then I'd like to query the database against that variable to fetch the name. Once that all works I'd like to respond back with the name of the user.
Can anyone help? It would be much appreciated as I continue my journey to learn Go!
答案1
得分: 19
我还没有使用过它,但是根据文档来看,这是你想要的吗?
db.QueryRow("SELECT name FROM users WHERE id=?", userId)
我猜想它应该以一种安全的方式将 ?
替换为 userId
。
http://golang.org/pkg/database/sql/#DB.Query
英文:
I haven't used it, but looking at the docs, is this what you are after?
db.QueryRow("SELECT name FROM users WHERE id=?", userId)
I assume it should replace the ?
with userId
in a sql safe way.
答案2
得分: 1
你可以尝试这样做。
var y string // 用于存储查询结果的变量。
err := db.QueryRow("SELECT name from user WHERE id = $1", jobID).Scan(&y)
if err != nil && err != sql.ErrNoRows {
fmt.Println(err)
}
文档参考:https://pkg.go.dev/database/sql#pkg-variables
英文:
You can try it this way.
var y string // Variable to store result from query.
err := db.QueryRow("SELECT name from user WHERE id = $1", jobID).Scan(&y)
if err != nil && err != sql.ErrNoRows {
fmt.Println(err)
}
Documentation reference: https://pkg.go.dev/database/sql#pkg-variables
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论