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

  • SEARCH
  • Home
  • 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 8449759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:47:50+00:00 2026-06-10T10:47:50+00:00

To insert data into a database using sqlite requires copying of the database to

  • 0

To insert data into a database using sqlite requires copying of the database to make it writable. So I have this method in my class which every suggestion I’ve seen over stack overflow and many other sites:

-(void)createEditableCopyOfDatabaseIfNeeded 
{  
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"sample.db"];

    success = [fileManager fileExistsAtPath:writableDBPath];

    if (success) return;
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"sample.db"];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
        if (!success) {
            NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
        }

}

Of course I have this next codes to access my database:

FMDatabase *db; = [FMDatabase databaseWithPath:[[NSBundle mainBundle] pathForResource:@"sample" ofType:@"db"]];
if (![db open]) {
    NSLog(@"Error!");
}
else {
    NSLog(@"Database opened!!");
}

[self createEditableCopyOfDatabaseIfNeeded];
[db executeUpdate:@"Insert into user (name, phone, comment) values(?, ?, ?)", @"Name1", @"13548", @"This is a comment", nil];
NSLog(@"Inserted");

FMResultSet *s = [db executeQuery:@"SELECT * FROM user"];
while ([s next]) {
    NSLog(@"%@", [s stringForColumn:@"comment"]);
}

Now, I have a few questions:

  1. The insertion code worked perfectly in the simulator when being executed/ran. However, this didn’t have an effect to the real sample.db which I concluded it must have a writable copy somewhere in the hard drive of my mac. That is acceptable. But when I ran these code on the iphone, it somewhat didn’t recognize my createEditableCopyOfDatabaseIfNeeded method and gave this error: Unknown error finalizing or resetting statement > (8: attempt to write a readonly database). How do I make these codes work using FMDB?

  2. When running on the iPhone, where would the be copied database located? After copied, does the iPhone use that database again even if not connected to my mac?

I cannot insert something when ran on the device.
Please help.

Thank you so much!

  • 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-06-10T10:47:52+00:00Added an answer on June 10, 2026 at 10:47 am

    if you are reading this, then I have the explanation to your problem.

    1. If there is any file in Resource Bundle, then it is a READ-ONLY file. So, your database path to your resources directory is available for reading purposes only.

    2. According to the code that you have written to copy the database, the database will be created in the your application’s document directory. This path will be unaffected even when you remove your iPhone from your Mac.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to insert data into the SQLite database using get EditText, but I
I am trying to insert a data into SQLite database using Python. INSERT INTO
I'm trying to insert non-latin data into sqlite database using bind variables using System.Data.SQLite.
I'm trying to insert some binary data into database using 'mysql' gem in ruby.
Getting a strange error when trying to insert data into an Access database using
I have following code for inserting data into database using PDO. It inserts data
I need to insert data into a column in the database. Which hook should
I have a function that inserts data into a SQLite database. I can manually
I want to insert data into my MySQL database using PowerShell. My code is
SQLite insert statement does not insert the new data i am using this code

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.