import "database/sql" import _ "github.com/go-sql-driver/mysql" func main() { db, err := sql.Open("mysql", "user:password@tcp(localhost:3306)/dbname") if err != nil { panic(err.Error()) } defer db.Close() // create table stmt, err := db.Exec("CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50), email VARCHAR(50))") if err != nil { panic(err.Error()) } fmt.Println(stmt) // insert user stmt, err = db.Exec("INSERT INTO users (name, email) VALUES (?, ?)", "John", "[email protected]") if err != nil { panic(err.Error()) } fmt.Println(stmt) }
import "database/sql" import _ "github.com/go-sql-driver/mysql" func main() { db, err := sql.Open("mysql", "user:password@tcp(localhost:3306)/dbname") if err != nil { panic(err.Error()) } defer db.Close() // delete user stmt, err := db.Exec("DELETE FROM users WHERE id=?", 1) if err != nil { panic(err.Error()) } fmt.Println(stmt) }Overall, the "database/sql" package provides a simple and efficient way to interact with various SQL databases using the DB Exec function.