2012-07-31 61 views
0

我是新的iPhone编程.. 我不断收到错误 ,我不知道如何更改权限的SQLite错误上的Xcode 4.3.2 <尝试写入只读数据库>

更新时出错。尝试写入只读数据库

和下面是我的编码

的NSString *文件路径= [[一个NSBundle mainBundle] pathForResource:@ “卡” ofType:@ “源码”];
sqlite3_stmt * statement;

如果(sqlite3_open([文件路径UTF8字符串],&分贝)== SQLITE_OK)
{
 常量字符* query_stmt = “UPDATE SET卡isActivate =?”;
 如果(sqlite3_prepare_v2(DB,query_stmt,-1,&声明,NULL)!= SQLITE_OK)
        {
                NSAssert1(0,@“错误,同时创造更新语句。'%s'“,sqlite3_errmsg(db));
       }
}


sqlite3_bind_int(语句,1,1);
如果(SQLITE_DONE = sqlite3_step(声明)!)
      {
         的NSLog( “在更新%s错误。” @,sqlite3_errmsg(DB));
     }

sqlite3_finalize(声明);
sqlite3_close(db);

希望有人可以帮助我,因为这真的让我感到沮丧..

回答

1

您需要在SQL文件复制到缓存目录,以便正确地使用它。
下面是我的一些代码:

NSString *sqlFile = @"database.sql"; 
NSArray *cachePath= NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); 
NSString *cacheDir = [cachePathobjectAtIndex:0]; 
databasePath = [cacheDirstringByAppendingPathComponent:sqlFile]; 

NSFileManager *fileManager = [NSFileManager defaultManager]; 
// Copy the database sql file from the resourcepath to the documentpath 
if (![fileManager fileExistsAtPath:databasePath]) { 
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:sqlFile]; 
    NSError *error; 
    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:&error]; 
    if (error != nil) { 
     NSLog(@"[Database:Error] %@", error); 
    } 
} 
+0

亚..我已经将数据库复制到我的目录.. 它可以完美的选择,但是当涉及到更新时,它只是不工作.. – Sunny 2012-07-31 09:20:22

+1

没有你的天堂”将其复制到您的文档目录中。你直接从你的包中打开数据库。这应该从缓存(或文档)目录中完成。这是两件不同的事情。 'NSCachesDirectory!= NSBundle' – basvk 2012-07-31 09:27:54

+1

Basvk说的是在你运行他给你的代码后,用NSString * filePath = databasePath替换你的文件路径字符串。 – 2012-07-31 14:59:37

相关问题