2012-06-21 82 views
0

希望这是有道理的。我的文档文件夹中有一个sqlite数据库。我还有一个函数,用于检查目录中是否存在该文件,如果不存在,它将从主包中移出。所以我知道数据库是在正确的地方访问。但是,当我运行我的应用程序时,我得到错误:没有这样的表:databaseName。问题可能与读取sqlite数据库的写入权限有关吗?如果是这样,我该如何检查并纠正问题?在目标文件中读取sqlite的写入权限c

#define kFilename @"foods.sqlite" 

- (NSString *)dataFilePath { 
NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
NSString* sqliteFile = [documentsPath stringByAppendingPathComponent:kFilename]; 
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:sqliteFile]; 
if(fileExists) 
    return sqliteFile; 
else { 

    NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:kFilename]; 
    NSString *folderPath = [documentsPath stringByAppendingPathComponent:kFilename]; 

    NSError *error; 

    [[NSFileManager defaultManager] copyItemAtPath:sourcePath 
              toPath:folderPath 
              error:&error]; 

    NSLog(@"Error description-%@ \n", [error localizedDescription]); 
    NSLog(@"Error reason-%@", [error localizedFailureReason]); 
} 
return [documentsPath stringByAppendingPathComponent:kFilename]; 
} 

-(void) viewWillAppear:(BOOL)animated{ 

sqlite3 *database; 
if (sqlite3_open([[self dataFilePath] UTF8String], &database) 
    != SQLITE_OK) { 

    sqlite3_close(database); 
    NSAssert(0, @"Failed to open database"); 
} 

sqlite3_stmt *statement; 
//why is this if statement failing? 

if (sqlite3_prepare_v2(database, [sqlStatement UTF8String], 
         -1, &statement, nil) == SQLITE_OK) { 
    while (sqlite3_step(statement) == SQLITE_ROW) { 
     //int row = sqlite3_column_int(statement, 0); 
     char *rowData = (char *)sqlite3_column_text(statement, 1); 
     foodName = [[NSString alloc] initWithUTF8String:rowData]; 
     [foodArray addObject:foodName]; 
    } 
    sqlite3_finalize(statement); 
} 
else { 

    NSLog(@"%i", sqlite3_prepare_v2(database, [sqlStatement UTF8String], 
           -1, &statement, nil)); 
    NSLog(@"Statement: %s", sqlite3_errmsg(database)); 

} 
sqlite3_close(database); 
} 
+0

你能告诉我们你的代码吗? – pasawaya

+0

刚刚发布的代码 –

回答

0

改用这种方法。跳过中间目录步骤,然后右对数据库。

- (void)checkAndCreateDB { 

    NSString *dbPath = [[[NSBundle mainBundle] bundlePath]stringByAppendingPathComponent:@"../Documents/yourDB.sqlite"]; 

    // Check if the SQL database has already been saved to the users phone, if not then copy it over 
    BOOL success; 

    // Check if the database has already been created in the users filesystem 
    success = [[NSFileManager defaultManager] fileExistsAtPath:dbPath]; 

    if(!success) { 

    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"yourDB.sqlite"]; 

    [fileManager copyItemAtPath:databasePathFromApp toPath:dbPath error:nil]; 

    NSLog(@"readandwrite is available"); 
    } 

if(!(sqlite3_open([dbPath UTF8String], &dB) == SQLITE_OK)) 
    { 
     NSLog(@"An error has occurred."); 
    } 

} 
0

您正在获取的错误表示数据库文件中不存在表'databaseName'。

您没有收到数据库无法打开的错误,所以这不是问题。

它看起来像你的sqlStatement是错误的。

+0

的SQL语句的格式是SELECT * FROM数据库WHERE foodType ='食物类型' 是不是正确的写法呢? –

+0

根据您的错误消息,该语句包括名为“databaseName”而不是“database”的表。尝试'NSLog(@“%@”,sqlStatement);'然后在尝试使用它之前查看它的实际情况。 – lnafziger

+0

我刚刚做到了。这是正确的说法 –