MySQLConnectorGo是GoogleGo编程语言连接MySQL数据库的驱动程序。
示例代码:
packagemainimport( "mysql"; "fmt";)//definemysqlinformationconst( hostname ="localhost"; username ="gotest"; password ="gotest"; database ="gotest";)funcmain(){ varquerystring; //connecttothedatabaseusingtheinformationdefinedabove db:=mysql.Connect(hostname,username,password,database); db.SelectDb("gotest"); //runanupdatequery query="UPDATE`gotest`SET`testfield`='Updatesomething'"; fmt.Println("Executingquery:",query); db.Query(query); //ifthequerywassuccessful,viewsomeinformation fmt.Println("Affectedrows:",db.AffectedRows,"InsertId:",db.InsertId,"\n"); //runaninsertquery query="INSERTINTO`gotest`SET`testfield`='Insertsomething',`testfield2`=12345.123,`testfield3`=NOW()"; db.Query(query); fmt.Println("Executingquery:",query); //ifthequerywassuccessful,viewsomeinformation fmt.Println("Affectedrows:",db.AffectedRows,"InsertId:",db.InsertId,"\n"); query="SELECT*FROM`gotest`"; db.Query(query); fmt.Println("Executingquery:",query); fmt.Println("Numrows:",db.NumRows()); for{ row:=db.FetchRow(); ifrow==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]); } //closetheconnection db.Close();}
评论