dounei9043 2015-08-20 17:50
浏览 162
已采纳

Golang mssql驱动程序返回“ mssql:无效的对象名称”

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条回答 默认 最新

  • doudang9147 2015-08-20 22:50
    关注

    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")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值