2010-04-19 98 views
0

嗨我有一个函数,基本上试图插入从REST调用返回的一些数据。Sqlite iPhone数据插入问题

- (void)syncLocalDatabase{ 
NSString *file = [[NSBundle mainBundle] pathForResource:@"pickuplines" ofType:@"db"]; 
NSMutableString *query = [[NSMutableString alloc] initWithFormat:@""]; 
sqlite3 *database = NULL; 
char *errorMsg = NULL; 
if (sqlite3_open([file UTF8String], &database) == SQLITE_OK) { 
    for(PickUpLine *pickupline in pickUpLines){ 
    [query appendFormat:@"INSERT INTO pickuplines VALUES(%d,%d,%d,'%@','YES')", pickupline.line_id, pickupline.thumbsUps, pickupline.thumbsDowns, [pickupline.line stringByReplacingOccurrencesOfString:@"'" withString:@"`"]]; 
    NSLog(query); 
    int result = sqlite3_exec(database, [query UTF8String], NULL, NULL, &errorMsg); 
    if (result!=SQLITE_OK) { 
    printf("\n%s",errorMsg); 
    sqlite3_free(errorMsg); 
    } 
    //sqlite3_step([query UTF8String]); 
    [query setString:@""]; 
    }//end for 
}//end if 
[query release]; 
sqlite3_close(database); } 

一切似乎罚款在日志语句中的查询字符串也很好,但数据没有被插入。作为select语句的这个函数的一个副本,其效果很好。 这里是计数器部分

- (void)loadLinesFromDatabase{ 

NSString *file = [[NSBundle mainBundle] pathForResource:@"pickuplines" ofType:@"db"]; 
sqlite3 *database = NULL; 
if (sqlite3_open([file UTF8String], &database) == SQLITE_OK) { 
    sqlite3_exec(database, "SELECT * FROM pickuplines", MyCallback, linesFromDatabase, NULL); 
} 
sqlite3_close(database); 
} 

我已经实现的回调&它工作正常。

我对Sqlite有点新,有人可以指出我做错了什么。 感谢名单

回答

0

您可以随时使用Crystalminds' SQLiteDatabase:
http://www.crystalminds.nl/?p=1342

他们做出了令人难以置信的容易使用的数据库库,所以没有更多的问题与困难的代码!我已经使用了一段时间了,我无法想象必须离开!