2013-01-22 96 views
-1

這是我的代碼: 它適用於其他方法,但僅在此創建問題。 在這裏,我無法插入數據... 這裏有什麼問題?代碼工作得很好。ios-issue與sqlite數據庫路徑

-(void) insertleadership { 
[self trunleadership]; 
// Setup the database object 
sqlite3 *database; 
NSLog(@"Inside leadership"); 
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) { 
    // Setup the SQL Statement and compile it for faster access 
    // NSString *query=[stringWithFormat: @"select name,password from customer where name=%@ and password=%@",*login_name,*login_pass]; 
    sqlite3_stmt *compiledStatement; 
    for(int i=0;i<ltitle.count;i++) 
    { 
     // NSString *insertSQL = [NSString stringWithFormat:@"insert into category values(\'aa\',\'bb\',\'cc\',\'cc\',\'cc\')"]; 
     // NSLog(@"Inside leadership"); 
     NSString *insertSQL = [NSString stringWithFormat:@"insert into leadership values(\'%@\',\'%@\',\'%@\',\'%@\',\'%@\',\'%@\',\'%@\',\'%@\',\'%@\',\'%@\',\'%@\',\'%@\',\'%@\')",[ltitle objectAtIndex:i],[ldate objectAtIndex:i],[lfunction objectAtIndex:i],[lservice objectAtIndex:i],[lsubservice objectAtIndex:i],[labstract objectAtIndex:i],[ldoctype objectAtIndex:i],[lcreated objectAtIndex:i],[lcorridor objectAtIndex:i],[lfeature objectAtIndex:i],[lindiastory objectAtIndex:i],[lpublish objectAtIndex:i],[ltopic objectAtIndex:i]]; 
     // NSLog(@"SQL Query for leader:%@",insertSQL); 
     const char *insert_stmt = [insertSQL UTF8String]; 
     // sqlite3_prepare_v2(databaseName, insert_stmt, -1, &statement, NULL); 
     sqlite3_prepare_v2(database, insert_stmt, -1, &compiledStatement, NULL); 
     if(sqlite3_step(compiledStatement) == SQLITE_DONE){ 
      NSLog(@"Insert into leadership successful"); 
     } 
     else { 
      NSLog(@"Unable to insert in leadership"); 
     } 
     // Release the compiled statement from memory 
     sqlite3_finalize(compiledStatement); 
    } 
    sqlite3_close(database); 
    } 
} 
+2

幾乎可以肯定你的「databasePath」是指包中的數據庫。該軟件包是隻讀的,在使用之前必須複製DB文件以讀/寫空間。 –

回答

1

如果你添加一些報告,以你的代碼,源碼本身會告訴你爲什麼它不開放:

if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) 
{ 
    // Do something 
    splite3_close(database); 
} 
else 
{ 
    NSLog(@"Failed to open database '%@': %s", databasePath, sqlite3_errmsg(database)); 
} 

而且@HotLicks是正確的,你應該複製數據庫退出應用除非您只讀(通過使用sqlite3_open_v2()並將SQLITE_OPEN_READONLY作爲其中一個標誌)打開它。