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.

http://golang.org/pkg/database/sql/#db.query


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -