mysql - Passing Variable to GoLang Query -
first off let me i'm beginner (started few days ago) golang , in process of learning how practically apply language. goal build web rest api queries database , provides data user.
i've been able create simple api using martini (https://github.com/go-martini/martini) , connect mysql database using https://github.com/go-sql-driver/mysql. problem @ current moment how pass variable param api request query. here 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 users id = userid").scan(&str) if err != nil && err != sql.errnorows { fmt.println(err) } return "hello " + str }) m.run() defer db.close() }
as can see goal take input variable (user) , insert variable called userid. i'd query database against variable fetch name. once works i'd respond name of user.
can help? appreciated continue journey learn go!
i haven't used it, looking @ docs, after?
db.queryrow("select name users id=?", userid)
i assume should replace ?
userid
in sql safe way.
Comments
Post a Comment