我想检查sqlite表上是否存在主键

时间:2011-03-29 07:21:41

标签: iphone objective-c database sqlite

我的Iphone应用程序上有一个sqlite数据库。该数据库有一个名为“Students”的表 它有10行数据,密钥从1到11.但我想测试目标c编码是否在表上存在值为“3”的主键。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

您需要对其运行选择查询。有关如何执行此操作的深入信息是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
相关问题