Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6751833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:58:26+00:00 2026-05-26T12:58:26+00:00

I am working on a Cocoa application which talks to a local SQLite database

  • 0

I am working on a Cocoa application which talks to a local SQLite database with FMDB. I ran into an issue that I can’t do any insert or update operation on DB. Select queries run perfectly fine, so I would assume my db connection settings are correct.

The structure of my code is basically like this:

FMDatabase* db=[FMDatabase databaseWithPath:[[NSBundle mainBundle] pathForResource:@"DBName" ofType:@"sqlite"]];

if(![db open])
{
  NSLog(@"Could not open db.");
}

db.traceExecution=YES;
[db beginTransation];
[db ExecuteUpdate:"INSERT INTO test (title) VALUES(?)", [NSNumber numberWithInt]:2],nil];
[db commit];
[db close];

No exceptions or warnings were thrown during execution, the console output regarding db.traceExecution is like following:

<FMDatabase: 0x100511fd0> executeUpdate: BEGIN EXCLUSIVE TRANSACTION;
<FMDatabase: 0x100511fd0> executeUpdate: INSERT INTO test (title) VALUES(?);
obj: 2
<FMDatabase: 0x100511fd0> executeUpdate: COMMIT TRANSACTION;

The testing database is simply just a one column table of INT type.

Everything looks fine except that the db file is not updated at all. It’s really confusing to me as the Select query works perfectly fine. I checked the path of the database, it is pointing to the right one. First I suspect it’s caused by file permission, but the issue remain the same even if I allowed everyone to be able to read/write.

I have been stucked with this problems for many hours and couldn’t find a proper solution. Can anyone shed some light on this? Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:58:27+00:00Added an answer on May 26, 2026 at 12:58 pm

    Databases in the bundle are read only. If the file doesn’t exist at the destination folder where you define, you should copy it from the bundle to the library or documents folder and then connect to that. That means it will copy on first use of that path.

    Here’s a function to ‘prepare’ the database by copying it to the destination from the bundle. It copies it to library (from my iOS app) but you can copy wherever you want. In my case, it was contacts.db.

    I called this method from ensureOpened.

    - (BOOL)ensureDatabasePrepared: (NSError **)error
    {
        // already prepared
        if ((_dbPath != nil) &&
            ([[NSFileManager defaultManager] fileExistsAtPath:_dbPath]))
        {
            return YES;
        }
    
        // db in main bundle - cant edit.  copy to library if !exist
        NSString *dbTemplatePath = [[NSBundle mainBundle] pathForResource:@"contacts" ofType:@"db"];
        NSLog(@"%@", dbTemplatePath);
    
        NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
        _dbPath = [libraryPath stringByAppendingPathComponent:@"contacts.db"];
    
        NSLog(@"dbPath: %@", _dbPath);
    
        // copy db from template to library
        if (![[NSFileManager defaultManager] fileExistsAtPath:_dbPath])
        {
            NSLog(@"db not exists");
            NSError *error = nil;
            if (![[NSFileManager defaultManager] copyItemAtPath:dbTemplatePath toPath:_dbPath error:&error])
            {
                return NO;
            }
    
            NSLog(@"copied");
        }    
    
        return YES;    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a Cocoa application into which I'd like to allow Mail messages
I'm working on a Cocoa application, and I've run into a situation where I
I'm working on a Cocoa application which will be used for a digital-signage/kiosk style
I want to ensure that my application doesn't have any UI freezes when working
I'm working (at least trying...) on a Cocoa application, which uses several custom Cocoa
I'm interested in working with Lucene from a Cocoa application. I'm aware that there
I've got an Objective-C/cocoa based application that I'm working on. This app is client<->server.
I'm working with CoreData in Cocoa (not document-based). My problem is, that I want
I've got a cocoa application that reads in a text file. I'd like to
I am working on a project that includes a Mac application and an iPad

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.