簡體   English   中英

我沒有這樣的桌子 <table name> 從sqlite檢索數據時出錯?

[英]Im getting no such table <table name> error when i retrieve the data from 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);
}

謝謝前進

我認為您的數據庫文件在捆綁中,而您正在從documentDirectory ..而不是捆綁中檢索數據庫!

您可以將路徑更改為

 NSString *documentPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"locations.sqlite"];
 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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM