2012-07-31 56 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

相關問題