本文实例讲述了go语言通过odbc操作Access数据库的方法。分享给大家供大家参考。具体如下:
这里需要用到go-odbc库,下载地址为:https://github.com/weigj/go-odbc
复制代码 代码如下:
package main;
import (
"fmt"
"database/sql"
_"odbc/driver"
)
func main(){
conn,err := sql.Open("odbc","driver={Microsoft Access Driver (*.mdb)};dbq=d:\\test.mdb");
if(err!=nil){
fmt.Println("Connecting Error");
return;
}
defer conn.Close();
stmt,err := conn.Prepare("select * from test");
if(err!=nil){
fmt.Println("Query Error");
return;
}
defer stmt.Close();
row,err := stmt.Query();
if err!=nil {
fmt.Println("Query Error");
return;
}
defer row.Close();
for row.Next() {
var id int;
var name string;
if err := row.Scan(&id,&name);err==nil {
fmt.Println(id,name);
}
}
fmt.Printf("%s\n","finish");
return;
}
import (
"fmt"
"database/sql"
_"odbc/driver"
)
func main(){
conn,err := sql.Open("odbc","driver={Microsoft Access Driver (*.mdb)};dbq=d:\\test.mdb");
if(err!=nil){
fmt.Println("Connecting Error");
return;
}
defer conn.Close();
stmt,err := conn.Prepare("select * from test");
if(err!=nil){
fmt.Println("Query Error");
return;
}
defer stmt.Close();
row,err := stmt.Query();
if err!=nil {
fmt.Println("Query Error");
return;
}
defer row.Close();
for row.Next() {
var id int;
var name string;
if err := row.Scan(&id,&name);err==nil {
fmt.Println(id,name);
}
}
fmt.Printf("%s\n","finish");
return;
}
希望本文所述对大家的Go语言程序设计有所帮助。