MySQL Connector Go
MySQL Connector Go 是 Google Go 编程语言连接 MySQL 数据库的驱动程序。
示例代码:
package main
import (
"mysql";
"fmt";
)
// define mysql information
const (
hostname = "localhost";
username = "gotest";
password = "gotest";
database = "gotest";
)
func main() {
var query string;
// connect to the database using the information defined above
db := mysql.Connect(hostname, username, password, database);
db.SelectDb("gotest");
// run an update query
query = "UPDATE `gotest` SET `testfield` = 'Update something'";
fmt.Println("Executing query: ", query);
db.Query(query);
// if the query was successful, view some information
fmt.Println("Affected rows: ", db.AffectedRows, "InsertId: ", db.InsertId, "\n");
// run an insert query
query = "INSERT INTO `gotest` SET `testfield` = 'Insert something', `testfield2` = 12345.123, `testfield3` = NOW()";
db.Query(query);
fmt.Println("Executing query: ", query);
// if the query was successful, view some information
fmt.Println("Affected rows:", db.AffectedRows, "InsertId:", db.InsertId, "\n");
query = "SELECT * FROM `gotest`";
db.Query(query);
fmt.Println("Executing query: ", query);
fmt.Println("Num rows: ", db.NumRows());
for {
row := db.FetchRow();
if row == nil {
break
}
fmt.Printf("(%T) %d => (%T) %s, (%T) %f, (%T) %+v\n", row[0], row[0], row[1], row[1], row[2], row[2], row[3], row[3]);
}
// close the connection
db.Close();
}