2

I an tasked with porting and Android app I built some months ago over to iOS.

Now, I have also worked with SQLite in iOS in the past. However, I remember it being a lot more work than the Android equivalent. Especially in relation to querying.

In Android I was able to perform a query using the following command (see below), where db is an instance of SQLite, dbName() is a method that returns the name of the DB, getFieldNames() returns a array of field name Strings and where contains the where clause.

Cursor result = db.query(dbName(), getDBFieldNames() , where,null,null,null,null);

I want to use such a nice and clean db.query command in the iOS version of SQLite3. However, does it (or something like it) exist, because I can't find it?

3 Answers 3

2

The difference is that you will be using the C api for sqlite, which is a bit more difficult to use than the Java one. It is certainly very different, so don't expect it to work identically.

You should check out the sqlite documentation for using sqlite with C/C++.

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

1 Comment

Thanks for the advice. I ended up completely re-implementing and came up with a great solution. Now I have an extensible class/object to provide simple objects an easy way to commit themselves to a dB. Cheers.
1

Kekoa is right. Alternatively, you can use FMDB in iOS, which gives you a nice (but still pretty lightweight) wrapper around SQLite. The API will be a little different than the one you're familiar with, but FMDB gets you out of the weeds of the SQLite C interface.


Thus, in FMDB, you can open a database with:

FMDatabase *db = [FMDatabase databaseWithPath:dbPath];

You can open your record set like so:

FMResultSet *rs = [db executeQuery:@"select rowid,* from test where a = ?", @"hi'"];

You can iterate through the rows of your result set like so:

while ([rs next]) {
    // just print out what we've got in a number of formats.
    NSLog(@"%d %@ %@ %@ %@ %f %f",
          [rs intForColumn:@"c"],
          [rs stringForColumn:@"b"],
          [rs stringForColumn:@"a"],
          [rs stringForColumn:@"rowid"],
          [rs dateForColumn:@"d"],
          [rs doubleForColumn:@"d"],
          [rs doubleForColumn:@"e"]);
}

And you can close your result set like so:

[rs close];  

1 Comment

I wish I could flag two answers. I gave you a point anyway. Thanks.
1

I've been working on a tool to help with simple access to a SQLite database on both iOS (with ARC) and Android. The tool takes in a sqlite database file and autogenerates base classes and some helper classes to handle the CRUD queries. There's also examples of how to use it for iOS and Android. I hope this can be helpful. https://github.com/pl4u/flyingdb

Comments

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.