慕仰8121524
Go语言操作数据库非常的简单,他也有一个类似JDBC的东西"database/sql"实现类是"github.com/go-sql-driver/mysql"使用过JDBC的人应该一看就懂对日期的处理比较晦涩,没有JAVA流畅:复制代码代码如下:package mainimport ( "database/sql" _ "github.com/go-sql-driver/mysql" "log" "time")/* create table t( id int primary key auto_increment, name varchar(20) not null, ts timestamp );*/func insert(db *sql.DB) { stmt, err := db.Prepare("insert into t(name,ts) values(?,?)") defer stmt.Close() if err != nil { log.Println(err) return } ts, _ := time.Parse("2006-01-02 15:04:05", "2014-08-28 15:04:00") stmt.Exec("edmond", ts)}func main() { db, err := sql.Open("mysql", "xx:xx@tcp(127.0.0.1:3306)/mvbox?charset=utf8") if err != nil { log.Fatalf("Open database error: %s\n", err) } defer db.Close() err = db.Ping() if err != nil { log.Fatal(err) }