简体   繁体   中英

Golang mssql driver returns “mssql: Invalid object name”

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?

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM