0

Ok so I understand that fmdb is totally outdated, and core data is where it's at, but for now, I just need to tweak an app I'm working on that uses fmdb to read from a sqlite db in the resources folder.

I need to be able to write to my db. What is the best way to execute queries? I have tried doing the same thing that I do to read from a db:

FMResultSet * set = [[UIApp database] executeQuery:[customQueries selectAll], book];

Where I change the string selectAll from being a SELECT statement into being a INSERT INTO statement and I pass it the variable book to insert, but that doesn't seem to be working...

What am I missing? What do I nee to do to be able to write to my sqlite db?? Thanks!

1
  • 2
    fmdb is not outdated, it's actively being developed. ;) Commented Nov 26, 2010 at 12:08

2 Answers 2

5

To insert you would do something like this, where db is a reference to your FMDatabase:

[db executeUpdate:@"insert into theTable (field1, field2) values(?, ?)", 
    theField1, theField2, nil];

Make sure there is a nil at the end of the parameter list (2 question marks in the query means two parameters and a nil), and make sure that you are using NS types as the parameters. If you try to use an int as a parameter, it will crash, you would need to use an NSNumber instead.

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

Comments

2
FMDatabase* db = [FMDatabase databaseWithPath:dbPath];

FMResultSet *rs = [db executeQuery:@"select * from table"];
while ([rs next]) {
    NSLog(@"%d", [rs intForColumn:@"id"];
}

// Close the result set.
[rs close];  

// Close the database.
[db close];

1 Comment

You forgot to open your database, but otherwise it still is not correct, as they are asking for a way to insert into and update the database, not select data records.

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.