0

I'm new to IOS development so I am following this

tutorial

As mentioned in this tutorial I made my database using the SQLITE command line, created my tables and next imported the database in my XCode 4.6 Project by adding it to Supporting Files folder.

I just want to populate the table with data so I have a function that first finds my database and copies it to the Documents folder (if not there, already):

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"Customers.db"];
FMDatabase* db = [FMDatabase databaseWithPath:writableDBPath];

This works fine as the writebleDBPath points to the actual path where Customers.db lies (in the project's Documents folder)

Now I open the database and try to add a new record:

[db open];
BOOL success =  [db executeUpdate:@"INSERT INTO customers (firstname,lastname) VALUES (?,?);",[patient.firstName UTF8String],[patient.secondName UTF8String], nil];

[db close];

but success value is always 'NO'.

I include the code used to create the sqlite database:

CREATE TABLE customers(id integer primary key, firstname varchar(30), lastname varchar(30))

What am I missing?

1
  • Try www.github.com/pmurphyjam/DBExample It's an Xcode project that uses Sqlite. The SQL syntax is the same as FMDB. It can also do large SQL transactions. Commented May 22, 2014 at 21:35

2 Answers 2

1

If id is a primary key, perhaps you need to set that, too.

Try invoking [db lastErrorMessage] to see what the problem is.

Sign up to request clarification or add additional context in comments.

Comments

0

You left out a step in the tutorial:

NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:self.databaseName];
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];

Which copies the database file from the bundle. This assumes you created the database and it's schema and included it in the package with the app.

3 Comments

Thanks, that's true. I managed to call a function before writing to the database, which checks if the databased has been copied into the documents folder, otherwise it copies it right away using the method copyItemAtPath you have just described. And it works! Now, I wonder if this is persistent, I mean do I have to perform additional actions to make the changes persistent on device? I found out that if I re-run the simulator - after running the app once and inserting into the database and closing the simulator - the app runs with a fresh copy of the database
You only need to do this step if the file doesn't exist. So wrap the above in a call to check for the file before copying it again.
Thank you Richard, I've just did that, and it works OK. However, I have just one more question, and I'm done: do I need something more to make the changes (inserts into table) persistent? I mean, I run the app in the simulator, I can insert as many times as I want into the table, I check that the items are inserted correctly. Everything seems fine. But then, I stop the run (from the XCode STOP button) and re-run/rebuild the app which opens in the same simulator as before, but now with an empty copy of the database. Why don't I see the inserts I've done in the first run?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.