代码之家  ›  专栏  ›  技术社区  ›  Cyril Gupta

MySql表示,对于非空的列,列不能为空![使用命名参数]

  •  4
  • Cyril Gupta  · 技术社区  · 17 年前

    INSERT INTO post (
    ID, content, post_url, blogID, title, addedOn, 
    updatedDate, commentsFeedURL, active, viewCount, 
    commentCount, languageID, authorName, postDate, 
    posRating, negRating, adult) 
    VALUES(
    @ID, @content, @post_url, @blogID, @title, @addedOn, 
    @updatedDate, @commentsFeedURL, @active, @viewCount, 
    @commentCount, @languageID, @authorName, @postDate, 
    @posRating, @negRating, @adult)
    

    但它不是空的。这是参数post_url中的值

    http://abcd.wordpress.com/2007/08/13/%e0%a4%a6%e0%a5%8b-%e0%a4%ae%e0%a4%bf%e0%a4%a8%e0%a4%9f-%e0%a4%95%e0%a4%be-%e0%a4%a7%e0%a5%8d%e0%a4%af%e0%a4%be%e0%a4%a8/

    cmd.Parameters.AddWithValue("post_url", postOld.URL);
    

    6 回复  |  直到 17 年前
        1
  •  7
  •   Cyril Gupta    17 年前

    问题很简单,在MySQL查询中,参数被标记为“?不是“@”。不幸的是,许多查询似乎都使用了“@”运行良好(事实并非如此),所以当出现问题时,人们会发现这一点。

    谢谢你的回答。我把我的问题改写成这样:

    INSERT INTO post (ID, content, post_url, blogID, title, addedOn, updatedDate, commentsFeedURL, active, viewCount, commentCount, languageID, authorName, postDate, posRating, negRating, adult)" +
                                " VALUES(?ID, ?content, ?post_url, ?blogID, ?title, ?addedOn, ?updatedDate, ?commentsFeedURL, ?active, ?viewCount, ?commentCount, ?languageID, ?authorName, ?postDate, ?posRating, ?negRating, ?adult)
    

        2
  •  0
  •   Rob Farley    17 年前

    在调用Execute方法之前,您能检查一下每个参数中的值吗?很容易不小心做了一些会搞砸的事情。

        3
  •  0
  •   George Mastros    17 年前

    那张桌子上有插入触发器吗?可能是触发器正试图插入到另一个表中,该表的post_url列不能为null。

        4
  •  0
  •   BlueMonkMN    17 年前

    尝试将post_url移动到参数和值列表中的内容之前,以排除驱动程序由于@content中的复杂值而将参数错误传递给MySQL的可能性。

        5
  •  0
  •   tpdi    17 年前

    这里不刻薄,但这就是为什么测试用例是一件好事。编写一个单元测试来验证传递非空姿势是否有效,传递空姿势将从数据库中得到此错误。

    然后编写一个集成测试(或模拟)来证明 postOld.URL

        6
  •  0
  •   Skyler    17 年前

    cmd.Parameters.AddWithValue("@post_url", postOld.URL);