dongyue0263 2012-12-18 10:32
浏览 274
已采纳

查询中的“列”IS NOT NULL在列不存在时不会抛出错误

In my PHP unit tests, I am using an SQLite in-memory database, which contains the following table:

CREATE TABLE "battlegroup_request" (
  "id" INTEGER NULL PRIMARY KEY AUTOINCREMENT,
  "battlegroupID" INTEGER NULL,
  "inviterID" INTEGER NULL,
  "inviteeID" INTEGER NULL
)

When I execute the following query with two integer bindings;

SELECT *
FROM "battlegroup_request"
WHERE "inviteeid" = ? AND
  "inviter" IS NOT NULL AND
  "battlegroupid" = ?
LIMIT 1

The query returns one result, but I expect it to throw an error, since the column inviter does not exist. Is this expected behaviour of SQLite? Am I doing something wrong?

I am using PDO, the connection was created as follows:

new PDO(
    'sqlite::memory:',
    null,
    null,
    array(
        PDO::ATTR_CASE => PDO::CASE_LOWER,
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,
        PDO::ATTR_STRINGIFY_FETCHES => false,
        PDO::ATTR_EMULATE_PREPARES => false,
    )
);

(These options are Laravel's default, which is the framework we're using.)

  • 写回答

1条回答 默认 最新

  • dqqlziv195281 2012-12-18 10:53
    关注

    If you use something like:

    SELECT "inviter" IS NOT NULL FROM battlegroup_request
    

    it will return TRUE if column inviter did not exist, because SQLite will treat "inviter" as simply string value (and it is certainly not NULL here).

    However, if you drop double quotes:

    SELECT inviter IS NOT NULL FROM battlegroup_request
    

    it will raise exception if column inviter did not exist.

    EDIT: Using backticks also forces SQLite to treat it as name (not a string):

    SELECT `inviter` IS NOT NULL FROM battlegroup_request
    

    will raise an exception if column inviter did not exist.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分