Writing to sqlite db using fmdb?
Ok so I understand that fmdb is totally outdated, and core data is whe开发者_Python百科re 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!
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.
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];
精彩评论