2012-08-17 36 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 
    } 
} 
相关问题