Function to use with MySQL
stmt, err := db.Prepare("INSERT INTO video VALUES(DEFAULT, DEFAULT, DEFAULT, DEFAULT)")
if err != nil {
log.Fatal(err)
}
res, err := stmt.Exec()
if err != nil {
log.Fatal(err)
}
lastID, err := res.LastInsertId()
if err != nil {
log.Fatal(err)
}
video.ID = lastID
//Select value
results, err := db.Query("SELECT name FROM users")
if err != nil {
panic(err.Error())
}
for results.Next() {
var user User
err = results.Scan(&user.Name)
if err != nil {
panic(err.Error())
}
fmt.Println(user.Name)
}
// Insert value
fmt.Println("Successfully Connected to MySQL database")
insert, err := db.Query("INSERT INTO users VALUES('1','ELLIOT')")
if err != nil {
panic(err.Error())
}
defer insert.Close()
func main() {
// Connect to the db
fmt.Println("Go MySQL Tutorial")
db, err := sql.Open("mysql", "root:@tcp(127.0.0.1:3306)/test")
if err != nil { //If probl with the connect
panic(err.Error())
}
defer db.Close()
/* Insert */
/* Select */
}