iphone - SQLite sqlite3_prepare_v2 没有错误但也没有

标签 iphone objective-c ios sqlite ios4

大家好,我只是在玩 SQLite,所以这对我来说是新的。我有一个 View ,可以在其中保存和查找人员数据。保存功能和查找功能完美运行。现在,我有了一个新 View ,里面有一个 tableView。我想获取联系人表中的所有人并用它填充列表。

到目前为止,我有:

-(void)viewWillAppear:(BOOL)animated {
    const char *dbpath = [databasePath UTF8String];
    sqlite3_stmt *statement;

    if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
    {
        NSLog(@"Opened DB");
        NSString *querySQL = [NSString stringWithFormat:@"SELECT name FROM contacts"];

        const char *query_stmt = [querySQL UTF8String];
        NSLog(@"could not prepare statement: %s\n", sqlite3_errmsg(contactDB));

        if (sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL) == SQLITE_OK)
        //if (sqlite3_step(query_stmt) == SQLITE_DONE)
        {
            //NSLog(@"SQLite OK");
            NSLog(@"SQLite ok");
            //if (sqlite3_step(statement) == SQLITE_ROW)
            //{
                while(sqlite3_step(statement) == SQLITE_ROW) {
                    NSLog(@"SQLite ROW");
                    NSString *person = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];
                    [personsList addObject:person];
                }
            //} else {
            //    NSLog(@"Emtpy list");
            //}
            sqlite3_finalize(statement);
        }
        sqlite3_close(contactDB);
    }
    NSLog(@"Calling reload");
    NSLog(@"%@", personsList);
    [tabelView reloadData];
}

这一切都在 viewWillAppear 中。加载并完成 View 后,日志显示: 2011-05-06 10:45:26.976 数据库[1412:207] 打开数据库

2011-05-06 10:45:26.978 数据库[1412:207] 无法准备语句:不是错误

2011-05-06 10:45:26.979 数据库[1412:207] 调用重新加载

2011-05-06 10:45:26.979 数据库[1412:207] (

)

所以看起来语句不是错误,但 sqlite3_prepare_v2 毕竟不是 SQL_OK。有什么帮助吗?

编辑:在之前的 View 中,创建人员的语句是:@"INSERT INTO CONTACTS (name, address, phone) VALUES (\"%@\",\"%@\",\"%@\")", name.text, address.text, phone.text];

找人语句为:@"SELECT address, phone FROM contacts WHERE name=\"%@\"", name.text];

这两条语句起作用,显示数据。

最佳答案

@joetjah 我用你的代码来获取结果并且工作正常

2011-05-06 16:03:00.076 SQLiteDemo[6454:207] Opened DB
2011-05-06 16:03:09.550 SQLiteDemo[6454:207] could not prepare statement: not an error
2011-05-06 16:03:24.685 SQLiteDemo[6454:207] SQLite ok
2011-05-06 16:03:26.229 SQLiteDemo[6454:207] SQLite ROW
2011-05-06 16:03:28.254 SQLiteDemo[6454:207] SQLite ROW
2011-05-06 16:03:34.461 SQLiteDemo[6454:207] Calling reload
2011-05-06 16:03:35.009 SQLiteDemo[6454:207] (
    rahul,
    sqlite
)


-(void)testFunction{
    const char *dbpath = [databasePath UTF8String];
    sqlite3_stmt *statement;
    sqlite3 *contactDB;
    NSMutableArray *personsList = [[NSMutableArray alloc ] initWithCapacity:0];
    if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
    {
        NSLog(@"Opened DB");
        NSString *querySQL = [NSString stringWithFormat:@"SELECT name FROM contacts"];

        const char *query_stmt = [querySQL UTF8String];
        NSLog(@"could not prepare statement: %s\n", sqlite3_errmsg(contactDB));

        if (sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL) == SQLITE_OK)
            //if (sqlite3_step(query_stmt) == SQLITE_DONE)
        {
            //NSLog(@"SQLite OK");
            NSLog(@"SQLite ok");
            //if (sqlite3_step(statement) == SQLITE_ROW)
            //{
            while(sqlite3_step(statement) == SQLITE_ROW) {
                NSLog(@"SQLite ROW");
                NSString *person = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];
                [personsList addObject:person];
            }
            //} else {
            //    NSLog(@"Emtpy list");
            //}
            sqlite3_finalize(statement);
        }
        sqlite3_close(contactDB);
    }
    NSLog(@"Calling reload");
    NSLog(@"%@", personsList);
    //[tabelView reloadData];
}

大家可以对比一下代码,可能的失败点可能是 1] 数据未正确保存,因此未获取任何数据。 2] databasePath 被设置为空白(我怀疑你得到 Open Db 日志)

表格联系人是使用“CREATE TABLE contacts (name text, age integer)”创建的 尝试一次,如果有效请告诉我。

关于iphone - SQLite sqlite3_prepare_v2 没有错误但也没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5908968/

相关文章:

iphone - 修改文件 iOS 中的数据(字节)

ios - UICollectionView 类引用委托(delegate)属性

iphone - 如何正确设置App Store链接?

iphone - 如何在 objective-c 中声明指针数组

iphone - initWithContentsOfURL 出现奇怪的错误

iphone - iOS 日文手写输入代码 请帮忙

IOS:NSUserDefault 用于 UIImage 数组

ios - iPhone SDK处理调用

objective-c - CoreGraphics 在白色 Canvas 上绘制图像

ios - 将 subview 添加到 ScrollView 时遇到问题