db.go 数据库操作的写法

conn,err:=connect(connString)

iferr !=nil{

print.Warningln("Open connection failed:", err)

continue

}

deferconn.Close()

print.Goodln("Connected to Server", host)

allDB,err:=listDB(conn)

iferr !=nil{

print.Warningln("listdb error", err)

continue

}






funcconnect(connStringstring) (*sql.DB,error) {

returnsql.Open("mssql", connString)

}

funclistDB(db*sql.DB) ([]string,error) {

stmt,err:=db.Prepare(allDBQuery)

iferr !=nil{

returnnil, err

}

deferstmt.Close()

rows,err:=stmt.Query()

iferr !=nil{

returnnil, err

}

deferrows.Close()

databases:=[]string{}

forrows.Next() {

vardatabaseNamestring

err = rows.Scan(&databaseName)

iferr !=nil{

returnnil, err

}

databases =append(databases, databaseName)

}

returndatabases,nil

}

你可能感兴趣的:(db.go 数据库操作的写法)