Golang and mssql

Hi,

my english is not the best so please forgive me my mistakes
I am or was a developer many many years ago.
now it has seized me again and i deal with golang and mssql.

I would like to do a pretty simple thing but somehow despite intensive search on google etc. you can not find the right solution.
Here is my goal.
I would like to access the golang my MS database and run a select statment. this statmant should begin at id 1 at the beginning and then go through in a loop to the end.

I think I’m doing something wrong with the statement.

Here is the CODE:
package main

import (
“database/sql”
“flag”
“fmt”
“log”

_ "github.com/denisenkom/go-mssqldb"

)

var (
debug = flag.Bool(“debug”, false, “enable debugging”)
password = flag.String(“password”, “”, “the database password”)
port *int = flag.Int(“port”, 1433, “the database port”)
server = flag.String(“server”, “”, “the database server”)
user = flag.String(“user”, “”, “the database user”)
)

func main() {

flag.Parse()

if *debug {
	fmt.Printf(" password:%s\n", *password)
	fmt.Printf(" port:%d\n", *port)
	fmt.Printf(" server:%s\n", *server)
	fmt.Printf(" user:%s\n", *user)
}

connString := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%d", *server, *user, *password, *port)
if *debug {
	fmt.Printf(" connString:%s\n", connString)
}
conn, err := sql.Open("mssql", connString)
if err != nil {
	log.Fatal("Open connection failed:", err.Error())
}
defer conn.Close()

idi := 1
stmt, err := conn.Prepare("select test from test.dbo.test where id =", idi)
if err != nil {
	log.Fatal("Prepare failed:", err.Error())
}
defer stmt.Close()

}
—END CODE

The Error
\test.go:42:27: too many arguments in call to conn.Prepare
have (string, int)
want (string)


do you have any ideas

best regards
Rene

Rene, check the docs (https://golang.org/pkg/database/sql/#DB.Prepare). Prepare just need one parameter. The steps are : 1)Prepare the SQl String you want to execute 2)Execute it using Execute/Query/QuerRow methods
It could be something like

stmt, err := conn.Prepare("select test from test.dbo.test where id = @id")
if err != nil {
	log.Fatal("Prepare failed:", err.Error())
}
defer stmt.Close()

rows, err := stmt.Query(idi)
if err != nil {
	log.Fatal("Statemnt Query failed:", err.Error())
}
defer rows.Close()

....

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.