Thursday, 15 September 2011

mysql - Passing Variable to GoLang Query -


Let me first talk that I'm starting (starting some days ago) and in the process of learning with Gollg How to apply language in practice, my goal is to create a Web Rest API that queries a database and returns the data back to the user.

I have successfully used Martini () to create a simple API and a MySQL database. My problem at the present moment is how to pass a variable param to my query from the API request. Here's 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 ("/ Username / user", function (Parameters martini.parms) string {var userId = params ["user"] err = db.QueryRow ("Choose user name where id = userId"). Scan (& amp; Str) if err! = Void & amp! = Sql.ErrNoRows {fmt.Println (error)} Return "Hello" + str} m.Run () defer db.Close ()}  

h Which you can see my goal is to take the input variables (user) and to put it in a variable called Ughraidi. Then I want to query the database against that variable to get the name. Once all works, I want to answer with the user's name.

Can anyone help? It will be highly appreciated because I continue my journey to learn!

I have not used it, but seeing the docs, what is it that you are doing after Are you

  db.QueryRow ("user name by selector name?", UserId)  

I think it's ? should be converted to a SQL safely from userID .


No comments:

Post a Comment