Raw SQL
Queryx provides direct support for executing raw SQL queries through the following methods.
Query
Query
returns all rows from the database.
go
type User struct {
Name string `db:"user_name"`
}
var users []User
err := c.Query("select name as user_name from users where id in (?)", []int64{1, 2}).Scan(&users)
type User struct {
Name string `db:"user_name"`
}
var users []User
err := c.Query("select name as user_name from users where id in (?)", []int64{1, 2}).Scan(&users)
Query One
QueryOne
returns at most a single row from the database.
go
var user struct {
ID int64 `db:"user_id"`
}
err = c.QueryOne("select id as user_id from users where id = ?", 1).Scan(&user)
var user struct {
ID int64 `db:"user_id"`
}
err = c.QueryOne("select id as user_id from users where id = ?", 1).Scan(&user)
Exec
Exec
for SQL statement that don't return data.
go
rowsAffected, err := c.Exec("update users set name = ? where id = ?", "test1", 1)
rowsAffected, err := c.Exec("update users set name = ? where id = ?", "test1", 1)