【问题标题】:Creating an SQLite table only if it doesn't already exist仅当 SQLite 表不存在时才创建它
【发布时间】:2011-04-12 14:24:27
【问题描述】:

我需要我的 PHP 应用程序能够创建一个 SQLite 表,但前提是它不存在。我该怎么办?

【问题讨论】:

标签: php sqlite


【解决方案1】:

你可以使用:

CREATE TABLE IF NOT EXISTS <name> (
  /* definition */
)

SQLite 支持 (http://www.sqlite.org/syntaxdiagrams.html#create-table-stmt)

【讨论】:

  • 刚试过。我得到:警告:SQLiteDatabase::queryExec() [sqlitedatabase.queryexec]: 在“NOT”附近。这是一个 PHP 应用程序。
  • 版本为3.6.20。代码如下: $query = "CREATE TABLE IF NOT EXISTS messages (content TEXT, author TEXT)"; $db->queryExec($query, $error) 或 die($error);
  • 对我来说就像一个魅力:sqlite> CREATE TABLE IF NOT EXISTS messages (content TEXT, author TEXT); sqlite> .tables 消息
  • 仍然收到警告。我暂时打算在queryExec之前使用@操作符。
  • @MPelletier:版本是 3.6.20。
【解决方案2】:
CREATE TABLE IF NOT EXISTS ...

【讨论】:

    【解决方案3】:

    使用IF NOT EXISTS

    【讨论】:

      猜你喜欢
      • 2011-05-05
      • 2011-09-03
      • 2021-08-28
      • 1970-01-01
      • 2011-02-18
      • 1970-01-01
      • 2012-10-05
      • 2022-11-29
      • 2019-07-12
      相关资源
      最近更新 更多