0

嗨朋友我最近学习了 sqlite。我在 sqlite 中使用下面的代码来表示名称是否存在,但我没有得到结果。请帮我。BOOL columnExists = NO;

        sqlite3_stmt *selectStmt;
        NSString *upperString = [[NSString alloc] initWithFormat:exptypeFld.text];
        NSString* changeString = [upperString uppercaseString];
        NSLog(@"changeString %@",changeString);
        [upperString release];
        const char *sqlStatement = [[NSString stringWithFormat:@"SELECT expensetype from expensetypes where upper(expensetype) = '%@'",changeString] UTF8String];
        NSLog(@"char is %s",sqlStatement);
        if(sqlite3_prepare_v2(db, sqlStatement, -1, &selectStmt, NULL) == SQLITE_OK)
        {
            NSLog(@"Same........");
            columnExists = YES;
        }
4

2 回答 2

0

您可能更喜欢使用现有的类来访问 SQLite 数据库。你可以在这里找到我写的一个例子:https ://github.com/AaronBratcher/ABSQLite

它具有用于以更传统的数据库方式访问 SQLite 的包装类,我觉得这让事情变得更容易。

于 2013-07-11T17:04:16.253 回答
0

您不需要调用sqlite3_step来实际执行查询。另外,此时数据库连接是否打开?完成后,您还应该完成声明。而且您不应该使用字符串格式将值绑定到查询。你应该使用sqlite3_bind_xxx.

NSString *upperString = exptypeFld.text; // no need for the string format
NSString* changeString = [upperString uppercaseString];
NSLog(@"changeString %@",changeString);

const char *sqlStatement = "SELECT expensetype from expensetypes where upper(expensetype) = ?";
NSLog(@"char is %s",sqlStatement);

sqlite3_stmt *selectStmt;
if(sqlite3_prepare_v2(db, sqlStatement, -1, &selectStmt, NULL) == SQLITE_OK) {
     sqlite3_bind_text(sqlStatement, 1, [changeString UTF8String], -1, SQLITE_TRANSIENT);
     sqlite3_step(sqlStatement); // you should check the result of this too
}
sqlite3_finalize(sqlStatement);

此代码假定数据库已打开。

于 2012-10-31T06:46:01.597 回答