Kentosh
Kentosh

Reputation: 11

How get user input in models for build query in Beego

For my project, I have to use a variable which contains user input for build my query in the models. The user input is recover in my login controller.

    username := l.GetString("username")

I need it in my query in the models for the WHERE

    sqldb.Open(os.Getenv("driverdb"), os.Getenv("paramsdb"))
    query := sqldb.QueryAssociativeArray("My query = dbrole.id WHERE login =" + pq.QuoteLiteral(username) + ";")
    sqldb.Close()

Upvotes: -1

Views: 100

Answers (1)

ziur
ziur

Reputation: 17

With Beego you have the option of using the ORM or a Raw query

Assuming you have the initial config of the model Registered for the ORM approach you do something like:

o := orm.NewOrm()
user := User{Username: "Kentosh"}

err := o.Read(&user)
fmt.Printf("ERR: %v\n", err)
fmt.Println(user)

If you want to do it with a Raw query you use it like:

var user User
num, err := o.Raw("SELECT id FROM user WHERE username = ?", "ketosh").Values(&user)
fmt.Println(user)

Upvotes: 0

Related Questions