我想检查sqlite表上是否存在主键
问题描述:
我的iPhone应用程序有一个sqlite数据库。该数据库有一个名为“学生”的表 ,它有10行数据,密钥从1到11.但是我想通过目标c编码测试表中是否存在具有值“3”的主键。我想检查sqlite表上是否存在主键
答
运行select查询,如果它没有返回记录,那么该记录不存在。
+0
谢谢,我明白了.. – 2011-03-31 04:47:35
答
您需要对其运行选择查询。有关如何做到这一点的深入信息是here。
的基本步骤是:
- 打开数据库
- 准备select语句
- 绑定主机值
- 评估准备好的声明
- 收拾
的代码看起来像这样
// 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
我假设你的意思是它有10行与键1至11但缺少一个键。 – JeremyP 2012-08-14 09:14:07