English 中文(简体)
Writing to sqlite db using fmdb?
原标题:

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!

问题回答

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];




相关问题
what is wrong with this mysql code

$db_user="root"; $db_host="localhost"; $db_password="root"; $db_name = "fayer"; $conn = mysqli_connect($db_host,$db_user,$db_password,$db_name) or die ("couldn t connect to server"); // perform query ...

Users asking for denormalized database

I am in the early stages of developing a database-driven system and the largest part of the system revolves around an inheritance type of relationship. There is a parent entity with about 10 columns ...

Easiest way to deal with sample data in Java web apps?

I m writing a Java web app in my free time to learn more about development. I m using the Stripes framework and eventually intend to use hibernate and MySQL For the moment, whilst creating the pages ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

How can I know if such value exists in database? (ADO.NET)

For example, I have a table, and there is a column named Tags . I want to know if value programming exists in this column. How can I do this in ADO.NET? I did this: OleDbCommand cmd = new ...

Convert date to string upon saving a doctrine record

I m trying to migrate one of my PHP projects to Doctrine. I ve never used it before so there are a few things I don t understand. In my current code, I have a class similar to this: class ...

热门标签