How to select SQLITE data to a class?

Discussion in 'iOS Programming' started by mikezang, Sep 8, 2010.

  1. mikezang macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #1
    I try to create a Sqlite3 wrapper class for easy to use.

    I can get the select result in NSArray with NSDictionary items using as code as below, my question is what I want to instead of NSMutableDictionary *row to MyDatatClass *row, and MyDataClass is not fixed, I want to this wrapper class can be used for all of my data class, what can I do?:
    Code:
    -(NSArray *) selectData:(NSString *) sql {
        NSMutableArray *rows = [NSMutableArray array];
    
        sqlite3_stmt *statement = NULL;
    	
        if (sqlite3_prepare_v2(database, [sql UTF8String], -1, &statement, NULL) == SQLITE) {
            while (sqlite3_step(statement) == SQLITE_ROW) {
                NSMutableDictionary *row = [[NSMutableDictionary alloc] init];
    
                [self copyValuesFromStatement:statement toRow:row];
                [rows addObject:row];
                [row release];
            }
        }
    
        sqlite3_finalize(statement);
        return rows;
    }
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
  3. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #3
    Do you mean this site New Home for FMDB ?
    it was stopped in 2008, are you sure?
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    Don't stop at the first google result. And read the page's links.

    If you did that, you'd see it as the second result in this search:
    http://www.google.com/search?client=safari&rls=en&q=fmdb&ie=UTF-8&oe=UTF-8

    And the google-code project was updated in July 2010:
    http://code.google.com/p/flycode/source/browse/trunk/fmdb

    If you read its Project Home page, you'll see it was moved to github, and last updated in Aug 2010.
     
  5. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #5
    Thanks, I browsed new page and I found text there "Formatting updates, along with a new method for getting a dictionary back from a result set. ", this result is what I got, I want to get a result with a custom class, not NSDictionary, may I make sense?
     
  6. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    If your question is: can I use FMDB to get a data set back from sqlite where the results are stored in my custom class, the answer is YES.

    FMDB is short on docs and examples, but you do get all the source code. If you look at the example app you should see how you get back values from a db and you can store them in a custom class if you want. Personally I almost always put them either directly into a dictionary or into a custom container class that uses a dictionary for storage. It's extremely flexible that way and matches the concept of a row in a db very closely.

    Also, fwiw, I haven't used the latest method that you mention to automatically get back a dictionary. I access the result set to read out its contents and then store them in the dictionary (usually).
     
  7. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #7
    Well, I also found there is a sample to wrapper SQLite3 and get data for Model in book "iPhone Advanced Projects", it is very simple, then FMDB has too many contents so the performance maybe not good...

    I want to know which way is better? FMDB or sample in that book?
     
  8. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #8
    I already told you. Use FMDB.

    That's my opinion.

    If you have a specific question ask.
     
  9. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #9
    You'll have to measure them yourself. On the devices that matter to you.

    You're the only one who can do this, because you're the only one who knows what all your data is, how it's represented, how it's indexed, how it's retrieved, and what all the relevant performance constraints are.

    You ask the question as if there's one answer. There isn't. Every situation is different. Data representations, indexes, select clauses, etc. all have an effect.
     
  10. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #10
    Thanks for this suggestion.

    You are right, I have to confirm it by myself.
     

Share This Page