测试代码如下:
```go
package main
import (
"fmt"
"database/sql"
_ "github.com/go-sql-driver/mysql"
)
type TestMysql struct {
db *sql.DB
}
func Init() (*TestMysql,error){
test := new(TestMysql);
db,err := sql.Open("mysql","test:123456@tcp(127.0.0.1:330 6)/test?charset=utf8");
if err!=nil {
fmt.Println("database initialize error : ",err.Error());
return nil,err;
}
test.db = db;
return test,nil;
}
func (test *TestMysql)Close(){
if test.db!=nil {
test.db.Close();
}
}
func (test *TestMysql)Insert(){
if test.db==nil {
return;
}
stmt,err := test.db.Prepare("insert into test.test(id,stat_date,,count)
values(4018922640424466,'2016-09-01 00:00:00.0',1) on duplicate
key update count=greatest(0,cast(count as signed)+1);");
if err!=nil {
fmt.Println(err.Error());
return;
}
defer stmt.Close();
stmt.Exec();
}
func main(){
for i :=0;i<20;i++ {
go func () {
test,_ := Init();
test.Insert();
}()
}
}
```
其中test表结构如下:
create table test(id bigint , stat_date date,count int,primary key(id,stat_date))
编译执行发现没反应,请大神们指教!!!