iOS Sqlite数据库未显示第一条记录

时间:2014-11-06 07:02:13

标签: ios iphone sqlite

我想获取插入sqlite数据库的所有记录:收藏夹表。 我写了以下代码:从数据库中获取所有记录。  它给了我记录,但第一条记录没有显示...

for ex:if table有3条记录         它只给我2条记录,第一条记录丢失了。  当我使用sqlite manager执行查询时,它会向我显示正确的3条记录..

if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
    {
        // Setup the SQL Statement and compile it for faster access
        NSString *sqlStatement = @"SELECT * FROM Favorite ;";
        sqlite3_stmt *compiledStatement;
         NSLog(@"Database returned error %d: %s", sqlite3_errcode(database), sqlite3_errmsg(database));

        if(sqlite3_prepare_v2(database, [sqlStatement UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK)
        {

            NSLog(@"srow==%d",sqlite3_step(compiledStatement));

            while(sqlite3_step(compiledStatement) == SQLITE_ROW)
            {


                // title
                NSString *fti=[NSString stringWithUTF8String:(char *) sqlite3_column_text(compiledStatement,0)];

                NSString * ftitle = [[NSString alloc]initWithFormat:@"%@",fti];
                [app.favdbTitle addObject:ftitle];
                NSLog(@"ttile==%@",ftitle);

                //desc
                NSString *fdes=[NSString stringWithUTF8String:(char *) sqlite3_column_text(compiledStatement,1)];

                NSString * fdesc = [[NSString alloc]initWithFormat:@"%@",fdes];
                [app.favdbDec addObject:fdesc];


                int favno=sqlite3_column_int(compiledStatement, 2);
                NSInteger fvno=favno;
                [app.favdbid addObject:[NSString stringWithFormat:@"%ld", (long)fvno]];

                //cat 1
                NSString *fca1=[NSString stringWithUTF8String:(char *) sqlite3_column_text(compiledStatement,3)];

                NSString * fcate1 = [[NSString alloc]initWithFormat:@"%@",fca1];
                [app.favcat1 addObject:fcate1];

               //cat 2
                NSString *fca2=[NSString stringWithUTF8String:(char *) sqlite3_column_text(compiledStatement,4)];

                NSString * fcate2 = [[NSString alloc]initWithFormat:@"%@",fca2];
                [app.favcat2 addObject:fcate2];

                //cat 2
                NSString *fca3=[NSString stringWithUTF8String:(char *) sqlite3_column_text(compiledStatement,5)];

                NSString * fcate3 = [[NSString alloc]initWithFormat:@"%@",fca3];
                [app.favcat3 addObject:fcate3];







            }

        }
        // Release the compiled statement from memory
        sqlite3_finalize(compiledStatement);

    }
    sqlite3_close(database);

1 个答案:

答案 0 :(得分:1)

问题在于,在while循环之前,您还有NSLog正在调用sqlite3_step。因此,此NSLog将检索第一条记录,然后while循环将在第二条记录处获取。

只需通过NSLog电话删除sqlite3_step,您就可以了。