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 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥30 数字信号处理实验报告
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥15 ensp路由器启动不了一直报#
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要