How to insert , select , update, delete in golang (mysql)

INSERT
 result, err := db.Exec(
    "INSERT INTO user_info (name, age) VALUES (?, ?)",
    "poni",
    25,
)

SELECT a row
var username, password string
row := db.QueryRow("SELECT `username`, `password` FROM `user_info` WHERE `id`= ?", 25)
err := row.Scan(&username, &password)
fmt.Println("your username is %s , password is %s", username, password)

SELECT rows
rows, err := db.Query("SELECT name FROM user_info WHERE age > ?", 16)
if err != nil {
    log.Fatal(err)
}

//要記得做  rows.Close()
defer rows.Close()
for rows.Next() {
    var name string

    if err := rows.Scan(&name); err != nil {
    log.Fatal(err)
    }
    fmt.Printf("%s is %d\n", name, age)
}
if err := rows.Err(); err != nil {
    log.Fatal(err) 
}

這個網誌中的熱門文章