Skip to content

Instantly share code, notes, and snippets.

@m0cchi
Last active October 28, 2017 01:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m0cchi/75e88f98c5e9390d6ed33f116464ed57 to your computer and use it in GitHub Desktop.
Save m0cchi/75e88f98c5e9390d6ed33f116464ed57 to your computer and use it in GitHub Desktop.
error
package main
import (
_ "database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
)
const SQL = "SELECT `name` FROM `users` WHERE id = :id"
type User struct {
name string `db:"name"` // => Name string `db:"name"`
}
func main() {
db, err := sqlx.Connect("mysql", "root:@unix(/tmp/mysql.sock)/foobar_m0cchi____duplicate_guard?parseTime=true")
if err != nil {
panic(err.Error())
}
defer db.Close()
stmt, err := db.PrepareNamed(SQL)
if err != nil {
panic(err.Error())
}
defer stmt.Close()
user := User{}
args := map[string]interface{}{"id": 1}
err = stmt.Get(user, args)
fmt.Println(user)
fmt.Println(err) // => sql: Scan error on column index 0: unsupported Scan, storing driver.Value type []uint8 into type *main.User
// successful case
stmt, err = db.PrepareNamed(SQL)
if err != nil {
panic(err.Error())
}
rows, err := stmt.Queryx(args)
if rows.Next() {
rows.Scan(&user.name)
}
fmt.Println(user)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment