PHP SQLITE PDO检查是否存在视图或表

时间:2015-10-16 09:08:47

标签: php sqlite pdo

显然,我有一个简单的问题。作为文档,我可以检查DataBase上是否存在TABLE或VIEWS。在我的应用程序中,我需要检查是否两者都已经在db中存在以返回自定义错误。问题是我可以检查是否有TABLE,但不是VIEWS。例如:

 **Working case**
 $checkTable = $this->db->query("SELECT * FROM sqlite_master WHERE name  ='$table' and type='table'", PDO::FETCH_ASSOC); 

 **Not working case**
 $checkTable = $this->db->query("SELECT * FROM sqlite_master WHERE name  ='$table' and type='table' OR type='views'", PDO::FETCH_ASSOC);

**Not working case**
 $checkTable = $this->db->query("SELECT * FROM sqlite_master WHERE name  ='$table' and type='views'", PDO::FETCH_ASSOC);

奇怪的是,如果我对视图运行查询,则会返回结果:

**Working case**
 $checkTable = $this->db->query("SELECT * FROM VIEWSpeople WHERE name   ='$person'", PDO::FETCH_ASSOC); 

关于sqlite_master的一般查询查找视图:

 **Working case**
 $checkTable = $this->db->query("SELECT * FROM sqlite_master WHERE name   ='$table'", PDO::FETCH_ASSOC); 

问题:为什么查询无法将视图识别为type ='views'? 谢谢你的时间。

1 个答案:

答案 0 :(得分:0)

视图的类型不是views,而是view

此外,您应该使用括号来确保以正确的顺序评估表达式:

... WHERE name ='$table' AND (type='table' OR type='view')

或者,使用IN:

... WHERE name ='$table' AND type IN ('table', 'view')