2012-08-17 58 views
2

我使用一個sqlite數據庫的項目。我可以做像SELECT這樣的查詢,但不可能做INSERT! 在模擬器上,INSERT正常工作。只要我在我的iPod上編譯,就會出現以下錯誤消息:「嘗試寫入只讀數據庫」sqlite ios:試圖寫一個只讀數據庫

認爲這是一個文件的權利我做了一個:chmod 777 mydatabase.sqlite 但這並沒有改變!

我也試過,因爲我已經在其他網站上閱讀並複製該文件以使用他的副本來擁有它,但無濟於事。

你有解決方案嗎? 親切。

PS:這裏是我的代碼:

for(NSDictionary *q in quotes) { 
    sqlite3_stmt *statement; 
    sqlite3 *contactDB; 
    const char *dbpath = [dbPath UTF8String]; 

    if (sqlite3_open_v2(dbpath, &contactDB, SQLITE_OPEN_READWRITE, NULL) == SQLITE_OK) 
    { 
     NSInteger identifiant = [[q objectForKey:@"id"] integerValue]; 
     NSString *texte = [q objectForKey:@"texte_english"]; 
     NSString *auteur = [q objectForKey:@"auteur"]; 
     NSString *date = [q objectForKey:@"date"]; 
     NSInteger auteurId = [[q objectForKey:@"auteur_id"] integerValue]; 
     NSInteger nbComments = [[q objectForKey:@"nb_comments"] integerValue]; 

     NSString *insertSQL = 
       [NSString stringWithFormat:@"INSERT INTO quotes (id, texte_english, auteur, date, auteur_id, nb_comments) VALUES (%d, \"%@\", \"%@\", \"%@\", \"%d\", \"%d\")", 
              identifiant, 
              texte, 
              auteur, 
              date, 
              auteurId, 
              nbComments]; 

     const char *insert_stmt = [insertSQL UTF8String]; 
     sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL); 

     if (sqlite3_step(statement) != SQLITE_DONE) 
     { 
      NSLog(@"ERREUR1: %s",sqlite3_errmsg(contactDB)); 
     } 
     sqlite3_finalize(statement); 
     sqlite3_close(contactDB); 
    } 
} 
+0

什麼是'dbpath'的價值? – 2012-08-17 05:40:42

+0

什麼是dbPath?如果它是應用程序包內的路徑,則無法寫入該目錄。如果您使用種子數據庫,則需要將其複製到另一個目錄(如文檔),並且只能從該目錄實際打開。 – 2012-08-17 05:41:18

+0

dbPath是:/var/mobile/Applications/E43E1EEC-3D82-4FD0-A5DC-61A3B173A22D/myapp.app/tq.sqlite – 2012-08-17 05:46:59

回答

0

我已經找到了解決辦法:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *dbPath = [documentsDirectory stringByAppendingPathComponent:@"tq.sqlite"]; 

所以,我有這樣的錯誤: 「沒有這樣的表:報價」 你知道嗎?

我希望它會有用!

7

所有你需要做的就是改變你保存SQLite文件的根目錄,這個文件目前是隻讀的。進行這些更改:

這些代碼添加到您的AppDelegate

.h文件中:

@property (nonatomic,retain) NSFileManager *fileMgr; 
@property (nonatomic,retain) NSString *homeDir; 
@property (nonatomic,retain) NSString *title; 

.m文件:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    [self checkAndCreateDatabase]; 
    return YES; 
} 

-(NSString *)GetDocumentDirectory{ 
    fileMgr = [NSFileManager defaultManager]; 
    homeDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; 
    return homeDir; 
} 

    -(void) checkAndCreateDatabase{ 
    BOOL success; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSString *databasePath = [self.GetDocumentDirectory stringByAppendingPathComponent:@"YOUR DATABASE NAME.sqlite"]; 
    success = [fileManager fileExistsAtPath:databasePath]; 
    if(success) { 
     NSLog(@"working"); 
     return;} 
    else{ 
     NSLog(@"notworking"); 
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"YOUR DATABASE NAME.sqlite"]; 
    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil]; 
    } 
} 

而且在SQLite class加上這些行:

.h文件:

@property (nonatomic,retain) NSFileManager *fileMgr; 
@property (nonatomic,retain) NSString *homeDir; 

.m文件:

-(NSString *)GetDocumentDirectory{ 
    fileMgr = [NSFileManager defaultManager]; 
    homeDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; 
    return homeDir; 
} 

以及在INSERT和SELECT做出這些改變:

NSString *dbPath = [self.GetDocumentDirectory stringByAppendingPathComponent:@"YOUR DATABASE NAME.sqlite"]; 
    const char *dbpath = [dbPath UTF8String]; 

希望它能幫助:)你之前

+0

當然有幫助!乾杯Rudi! – Fischer 2013-01-31 01:24:11

1
-(void)createEditablecopyofDatabaseifneeded 
{ 
    BOOL success; 
    NSError *error; 
    NSFileManager *filemanagr = [NSFileManager defaultManager]; 

    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *dbPath = [path objectAtIndex:0]; 
    NSString *finalDBPath = [dbPath stringByAppendingPathComponent:mDataBaseName]; 
    self.mDataBasePath=finalDBPath; 
    success = [filemanagr fileExistsAtPath:finalDBPath]; 
    if (success) { 

    } 
    else 
    { 
     NSString *writableDBPath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:mDataBaseName]; 
     success = [filemanagr copyItemAtPath:writableDBPath toPath:finalDBPath error:&error]; 
     self.mDataBasePath=writableDBPath; 
    } 
    if (!success) { 

    } 
} 

檢查這在文檔目錄中創建數據庫 希望這一點將幫助你

0

如果有人看到這個......我開始在升級到Xcode 5.2後在我的一臺設備(iPhone 4s/iOS7)上出現這個錯誤。它沒有發生在我的iPhone 5(iOS 6)上。它發生在從商店獲取元數據時:

NSDictionary *storeMetadata = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType:NSSQLiteStoreType URL:storeURL error:&error]; 

刪除我的應用程序並重新安裝它解決了問題。使用

0

斯威夫特3 GRDB

var dbQueue: DatabaseQueue! 

func setUpDatabasePath() 
{ 
    let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! as NSString 
    let databasePath = documentsPath.appendingPathComponent("your_database.sqlite") 
    print("DATABASE PATH !!!!") 
    print(databasePath) 
    let fileManager:FileManager = FileManager.default 
    var success = fileManager.fileExists(atPath: databasePath) 
    if (success) 
    { 
     dbQueue = try! DatabaseQueue(path: databasePath) 
     print("writing to documents directory") 
     print(databasePath) 
     //break 
     return 
    } 
    if (!success) 
    { 
     let bundlePath = Bundle.main.path(forResource: "your_database", ofType: "sqlite") 
     success = fileManager.fileExists(atPath: bundlePath!) 
     print("writing from app bundle") 
     if (success) 
     { 
      try! fileManager.copyItem(atPath: bundlePath!, toPath: databasePath) 
      dbQueue = try! DatabaseQueue(path: databasePath) 
     } 
     else 
     { 
      print("Could not find database") 
     } 
     return 
    } 
} 
相關問題