0

我为表和数据库提供了相同的名称。但是我没有找到这样的表错误。

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);
}

感谢提前


我认为您的数据库文件在捆绑包中,并且您正在从 documentDirectory.. 而不是从捆绑包中检索数据库!!!!

您可以将路径更改为

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

2 回答 2

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], &dbConnection);

于 2013-02-13T07:10:31.887 回答
0

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

您可以将路径更改为

 NSString *documentPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"locations.sqlite"];
于 2013-02-13T06:28:28.837 回答