3

In an application I have a globally scoped

var db *sql.DB

that is later called with

slcstrSource, slcint64Timestamp, slcstrContent, err := DB_functions.GetContent(db)
            if err != nil {
                 fmt.Println("Error: " + err.Error())
            }

GetContent is this:

func GetContent(db *sql.DB) ([]string, []int64, []string, error) {

    var slcstrContent []string
    var slcint64Timestamp []int64
    var slcstrSource []string

    // Run the query
    rows, err := db.Query("SELECT source, timestamp, content FROM MyDatabase.MyTable")
    if err != nil {
            return slcstrSource, slcint64Timestamp, slcstrContent, err
    }
    defer rows.Close()

    for rows.Next() {

            // Holding variables for the content in the columns
            var source, content string
            var timestamp int64

            // Get the results of the query
            err := rows.Scan(&source, &timestamp, &content)
            if err != nil {
                    return slcstrSource, slcint64Timestamp, slcstrContent, err
            }

            // Append them into the slices that will eventually be returned to the caller
            slcstrSource = append(slcstrSource, source)
            slcstrContent = append(slcstrContent, content)
            slcint64Timestamp = append(slcint64Timestamp, timestamp)
    }

    return slcstrSource, slcint64Timestamp, slcstrContent, nil
}

When I run the application and these sections of code are hit, I get an:

Error: mssql: Invalid object name 'MyDatabase.MyTable'.

When I db.Ping() the database, it seems to work. From what I've narrowed down the error is happening right at the query, but I can't find what's wrong. I checked the database and there is a database called MyDatabase with a table called MyTable and the table has information in those three columns...

Is there something I'm missing before making the query, or in making the query?

1 Answer 1

5

I checked the database and there is a database called MyDatabase with a table called MyTable and the table has information in those three columns...

It seems like the driver is working just like it should. In order to query a table in SQL Server you should use [Database].[Schema].[TableName]. If you have not defined a particular schema name for your table then it will be created under the dbo schema by default. In saying that you don't really need to specify the database name in your query. You rather define that on the connection string. I'm not sure how you have defined your connection details but have a look at the below and adapt accordingly to your needs.

var (
    debug = flag.Bool("debug", false, "enable debugging")
    password = flag.String("password", "mypwd", "the database password")
    port *int = flag.Int("port", 1433, "the database port")
    server = flag.String("server", "MyServer", "the database server")
    user = flag.String("user", "MyUser", "the database user")
    connStr = fmt.Sprintf("server=%s;Initial Catalog=MySchema;userid=%s;password=%s;port=%d", *server, *user, *password, *port)
        )

    func main() {
        db, err := sql.Open("mssql", connStr)
    }

Then you can query your table like this:

rows, err := db.Query("SELECT source, timestamp, content FROM MySchema.MyTable")
Sign up to request clarification or add additional context in comments.

1 Comment

That seems to work now; it was strange to me because I was using a "test application" while creating the package, and it used the same method of querying the database as the application I was actually using the package in but the application failed while the test application worked! Not sure how it knew the proper schema and database. I'll have to keep poking. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.