sqlite ios : attempt to write a readonly database

前端 未结 5 1483
天涯浪人
天涯浪人 2021-01-15 05:52

I use a sqlite database for a project. I can do queries like SELECT but impossible to do INSERTs! On the simulator the INSERT works properly. As soon as I compile on my iPod

相关标签:
5条回答
  • 2021-01-15 06:13

    I have found the solution :

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

    So, i have this error : "no such table: quotes" Any idea ?

    I hope it will be useful !

    0 讨论(0)
  • 2021-01-15 06:23

    In case anyone else sees this...I started getting this error on one of my devices (iPhone 4s/iOS7) after upgrading to Xcode 5.2. It didn't happen on my iPhone 5 (iOS 6). It occurred when getting metadata from the store:

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

    Deleting my app and reinstalling it fixed the problem.

    0 讨论(0)
  • 2021-01-15 06:24

    Swift 3 using 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
        }
    }
    
    0 讨论(0)
  • 2021-01-15 06:29

    All you need to do is change the root that you are saving your SQLite file in you phone which is readonly at the moment. make these changes :

    Add these codes to your AppDelegate :

    .h file:

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

    .m file:

    - (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];
        }
    }
    

    Also in your SQLite class add these lines :

    .h file:

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

    .m file:

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

    and also in INSERT and SELECT make these changes :

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

    Hope it helps :)

    0 讨论(0)
  • 2021-01-15 06:30
    -(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) {
    
        }
    }
    

    Check this before you create the DB in the document directory Hope this will help you

    0 讨论(0)
提交回复
热议问题