美文网首页
golang adodb mssql数据库的query格式化奇葩

golang adodb mssql数据库的query格式化奇葩

作者: 毛毛v5 | 来源:发表于2019-05-13 15:26 被阅读0次

    用adodb驱动查询mssql数据。如果参数带有大括号。就会显示错误:
    ServeSrs sql db.Prepare error发生意外。 (语法错误或违反访问规则)
    代码如下:

        db, err0 := sql.Open("adodb", cfg.Cfg["mssql"])
        if err0 != nil {
            log.Println("ServeSrs sql open error")
        } else {
            defer db.Close()
            stmt1, err0 := db.Prepare(`
                select top ? *
                from (select a.[Id], a.[CreateDate],a.[PublisherId],a.[NickName],a.[FileSize],a.[Followed],
                a.[Readed],a.[FilePath], b.[STitle],row_number() over(order by a.CreateDate desc) as rownumber
                from [hds12204021_db].[dbo].[Web2019_historylist] a, [hds12204021_db].[dbo].[Web2019_roomlist] b
                where a.roomid = ? and a.Deleted = 0 and a.roomid=b.id ) temp_row
                where rownumber>((?-1)*?)
            `)
            if err0 != nil {
                log.Println("ServeSrs sql db.Prepare error" + err0.Error())
            } else {
                defer stmt1.Close()
                rows, err := stmt1.Query(pagesize, roomid, pageid, pagesize)
    
    

    然后我们把Prepare格式化的字符串,换个方式搞下:

        db, err0 := sql.Open("adodb", cfg.Cfg["mssql"])
        if err0 != nil {
            log.Println("ServeSrs sql open error")
        } else {
            defer db.Close()
            sqlstr := `
                select top ` + pagesize + ` *
                from (select a.[Id], a.[CreateDate],a.[PublisherId],a.[NickName],a.[FileSize],a.[Followed],
                a.[Readed],a.[FilePath], b.[STitle],row_number() over(order by a.CreateDate desc) as rownumber
                from [hds12204021_db].[dbo].[Web2019_historylist] a, [hds12204021_db].[dbo].[Web2019_roomlist] b
                where a.roomid = '` + roomid + `' and a.Deleted = 0 and a.roomid=b.id ) temp_row
                where rownumber>((?-1)*?)
            `
            //log.Println(sqlstr)
            stmt1, err0 := db.Prepare(sqlstr)
            if err0 != nil {
                log.Println("ServeSrs sql db.Prepare error" + err0.Error())
            } else {
                defer stmt1.Close()
                rows, err := stmt1.Query(pageid, pagesize)
    

    OK,先自己拼接字符串,再喂给mssql 的adodb驱动就行了。。这坑太奇葩。。

    相关文章

      网友评论

          本文标题:golang adodb mssql数据库的query格式化奇葩

          本文链接:https://www.haomeiwen.com/subject/iyalaqtx.html