golang操作mysql,执行DDL操作;
案例如下
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
)
func main() {
// Open a connection to the MySQL database
db, err := sql.Open("mysql", "username:password@tcp(localhost:3306)/database_name")
if err != nil {
panic(err.Error())
}
defer db.Close()
// Execute the TRUNCATE TABLE command
_, err = db.Exec("TRUNCATE TABLE table_name")
if err != nil {
panic(err.Error())
}
fmt.Println("Table truncated successfully.")
}
不过服务器一般无法连接外网,可以把mysql驱动下载下来,放入GOROOT
Note that you will need to replace username
, password
, localhost
, 3306
, database_name
, and table_name
with your own values. Also, make sure to import the github.com/go-sql-driver/mysql
package for the MySQL driver.