【问题标题】:I want to check whether a primary key exists on sqlite table我想检查sqlite表上是否存在主键
【发布时间】:2011-07-25 01:23:42
【问题描述】:

我的 Iphone 应用上有一个 sqlite 数据库。该数据库有一个名为“学生”的表 它有 10 行数据,键从 1 到 11。但我想通过客观 c 编码测试表上是否存在值为“3”的主键。

【问题讨论】:

  • 我假设你的意思是它有 10 行,键为 1 到 11,但缺少一个键。

标签: iphone objective-c database sqlite


【解决方案1】:

运行选择查询,如果它没有返回记录,则该记录不存在。

【讨论】:

    【解决方案2】:

    您需要对其运行选择查询。有关如何执行此操作的详细信息是 here

    基本步骤是:

    • 打开数据库
    • 准备一个选择语句
    • 绑定主机值
    • 评估准备好的语句
    • 收拾整理

    代码看起来像这样

    // Database already opened
    // All error checking omitted because I am lazy
    
    sqlite3_stmt statement;
    // Replace columns below with the names of your actual columns
    // and key with the name of the primary key column
    errorResult = sqlite3_prepare_v2(dbConnection, 
                                     "select columns from students where key = ?",
                                     -1,
                                     &statement,
                                     NULL);
    // error check and handle any
    errorResult = sqlite3_bind_int(statement, 1, 3); // Put 3 in place of first question mark
    // error check and handle any
    while ((errorResult = sqlite3_step(statement) == SQLITE_ROW)
    {
        // Use sqlite3 column functions to get data 
        // http://www.sqlite.org/c3ref/column_blob.html
    }
    // error check and handle any
    errorCheck = sqlite3_finalize(statement);
    // error check and handle any
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-16
      • 1970-01-01
      • 2021-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多