当我从sqlite中检索数据时,我没有得到这样的表错误?<table name =“”> </table>

时间:2013-02-13 05:54:18

标签: iphone ios ipad sqlite

我给表和数据库命名相同。但是我没有找到这样的表错误。

sqlite3 *dbConnection = nil;
NSArray *documentDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [documentDirectory objectAtIndex:0];
NSString *documentPath = [path stringByAppendingPathComponent:@"locations.sqlite"];
sqlite3_open([documentPath UTF8String], &dbConnection);

sqlite3_stmt *statement = nil;

@try {
    if(sqlite3_open([documentPath UTF8String], &dbConnection)!=SQLITE_OK)
    {
        sqlite3_close(dbConnection);
    }
    else
    {

        //create editable database copy

    }
    NSString *selectQuery=[[NSString alloc] initWithFormat:@"select * from address_locations"];
    const char * retrieveQuery = [selectQuery UTF8String];

    if(sqlite3_prepare_v2(dbConnection,retrieveQuery, -1, &statement, nil)==SQLITE_OK)
    {
        while (sqlite3_step(statement)==SQLITE_ROW)
        {
            NSMutableDictionary * dictionaryAboutInfo = [[NSMutableDictionary alloc]init];
            [dictionaryAboutInfo setValue:[NSString stringWithFormat:@"%s",sqlite3_column_text(statement, 0)] forKey:@"ZIP"];
            [dictionaryAboutInfo setValue:[NSString stringWithFormat:@"%s",sqlite3_column_text(statement, 1)] forKey:@"LATITUDE"];
            [dictionaryAboutInfo setValue:[NSString stringWithFormat:@"%s",sqlite3_column_text(statement, 2)] forKey:@"LONGITUDE"];
            ////NSLog(@"Retrived Dict values %@",dictionaryAboutInfo);
            [dealersInfoArray addObject:dictionaryAboutInfo];
            [dictionaryAboutInfo release];
        }
    }
    [selectQuery release];
}
@catch (NSException *exception) {

}
@finally {
    sqlite3_finalize(statement);
    sqlite3_close(dbConnection);
}

感谢您提前

2 个答案:

答案 0 :(得分:0)

我认为您的数据库文件在bundle中,而您正在从documentDirectory中检索数据库..而不是从bundle !!!!

您可以将路径更改为

 NSString *documentPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"locations.sqlite"];

答案 1 :(得分:0)

 BOOL  success;
NSFileManager *fileManager=[NSFileManager defaultManager];
success = [fileManager fileExistsAtPath:documentPath];
if (success) 
{
    NSLog(@"file found at the document path"); 
}
//bundle path
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"address_locations.sqlite"];
[fileManager copyItemAtPath:databasePathFromApp toPath:documentPath error:nil];

sqlite3_open([documentPath UTF8String],&amp; dbConnection);