SQLite数据插入没有反映在表中

时间:2010-12-27 07:33:41

标签: sqlite uitableview iphone-sdk-3.0

我可以插入我的数据,但我无法在表格视图中显示它们。我试过[tableview reloadData] 但没有成功

这是我的代码:

-(void)gButtonTapped:(id)sender
{  


    NSLog(@"right nav bar button is  hit%@ ",storePaths);
    //[self readAnimalsFromDatabase2];

    appDelegate = (DatabaseTestAppDelegate *)[[UIApplication sharedApplication] delegate];
    sqlite3 *database;

     sqlite3_stmt *compiled_statement1;
     if(sqlite3_open([storePaths UTF8String], &database) == SQLITE_OK) {
         //const char *sqlStatement = 


         NSString *newQuery = [NSString stringWithFormat:@"insert into cat_tbl (cat_id,names,imgs) values ('12','test1','r.png')"];

        // NSString *newQuery = [NSString stringWithFormat:@"select * from list_tbl"];

        const char *sql = [newQuery cStringUsingEncoding:NSASCIIStringEncoding];


        NSLog(@"update query is %@",newQuery);

        if(sqlite3_prepare_v2(database, sql, -1, &compiled_statement1, NULL) == SQLITE_OK)
        {
            int result = sqlite3_step(compiled_statement1);
            sqlite3_reset(compiled_statement1);
            NSLog(@"result %d", result);

            if(result != SQLITE_ERROR) {
                int lastInsertId =  sqlite3_last_insert_rowid(database);
                NSLog(@"x %d", lastInsertId);
        }




        }

    }

    sqlite3_finalize(compiled_statement1);


    sqlite3_close(database);
    [tableView reloadData];// this is also  not working 

}

1 个答案:

答案 0 :(得分:0)

您需要通过调用select查询来更新表视图数据源,或者通过在数据库中成功插入新值来更新数据源。