【问题标题】:sqlite - how to check if table exists before completing inserts? [duplicate]sqlite - 如何在完成插入之前检查表是否存在? [复制]
【发布时间】:2012-05-05 22:52:32
【问题描述】:

让 SQLite 数据库有效执行的最佳 SQL 是什么:

If Database Table Exists then
  - create table
  - insert row
  - insert row (i.e. for startup data)
end

【问题讨论】:

标签: sqlite


【解决方案1】:

要检查您的表是否存在,您可以使用:

SELECT * FROM sqlite_master WHERE name ='myTable' and type='table'; 

【讨论】:

  • 好的 - 所以你认为最好将插入保持为单独的 sql 语句,我假设?谢谢
  • 对于 TEMP 表,必须查看 sqlite_temp_master。
【解决方案2】:

您可以让 Sqlite 自己为您检查:

CREATE TABLE IF NOT EXISTS <table_name> ...;    

点击文档链接: https://sqlite.org/lang_createtable.html

【讨论】:

  • 就是这样!
  • 对数据库本身来说是的,但是如果我在调用它之前在应用程序中有很多事情要做呢?如果我能够提前检查,我可以节省计算时间和资源。
  • 这是正确答案!
【解决方案3】:

使用此代码

SELECT name FROM sqlite_master WHERE type='table' AND name='yourTableName';

如果返回数组计数等于1,则表示表存在否则不存在。

【讨论】:

    猜你喜欢
    • 2015-12-17
    • 1970-01-01
    • 1970-01-01
    • 2019-05-12
    • 1970-01-01
    • 2012-02-02
    • 1970-01-01
    • 2011-01-22
    • 1970-01-01
    相关资源
    最近更新 更多