Last active
October 28, 2017 01:33
-
-
Save m0cchi/75e88f98c5e9390d6ed33f116464ed57 to your computer and use it in GitHub Desktop.
error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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