【发布时间】:2011-07-25 01:23:42
【问题描述】:
我的 Iphone 应用上有一个 sqlite 数据库。该数据库有一个名为“学生”的表 它有 10 行数据,键从 1 到 11。但我想通过客观 c 编码测试表上是否存在值为“3”的主键。
【问题讨论】:
-
我假设你的意思是它有 10 行,键为 1 到 11,但缺少一个键。
标签: iphone objective-c database sqlite
我的 Iphone 应用上有一个 sqlite 数据库。该数据库有一个名为“学生”的表 它有 10 行数据,键从 1 到 11。但我想通过客观 c 编码测试表上是否存在值为“3”的主键。
【问题讨论】:
标签: iphone objective-c database sqlite
运行选择查询,如果它没有返回记录,则该记录不存在。
【讨论】:
您需要对其运行选择查询。有关如何执行此操作的详细信息是 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
【讨论】: