开发者

Does SQLite support "datareader"?

I'm trying to use a datareader in SQLite, but am unable to find anything in the开发者_运维知识库 docs I have ("Using SQLite" by Kreibich).

Can someone tell me if it's supported and where I can find some examples?


Yes, you just need to get yourself System.Data.SQLite.

It comes in two variants, one that has SQLite built-in, and another which requires that you also ship a separate native sqlite DLL.


the sqlite api has a concept which is logically equivalent to the .net reader. which means, you issue a query and then iterate read data as needed. that keeps memory low as your not pulling the complete result set into memory.

first of all, take a look at other wrappers like fmdb.

here's the equivalent using the c api inside of iPhone. You prepare the statement by passing the sql query (sqlite parses under the cover), then you call step which is the equivalent to the .net reader read method. The you read columns just like the .net data reader. Note that this example prepares and finalizes (cleans up). A more efficient approach is to save the prepared statement and then call reset to avoid having sqlite parse the query over and over.

// prep statement
sqlite3_stmt    *statement;
NSString *querySQL = @"SELECT id, name FROM contacts";
NSLog(@"query: %@", querySQL);
const char *query_stmt = [querySQL UTF8String];

// preparing a query compiles the query so it can be re-used.
sqlite3_prepare_v2(_contactDb, query_stmt, -1, &statement, NULL);    

// process result
while (sqlite3_step(statement) == SQLITE_ROW)
{
    int idx = 0;
    Contact *contact = [[Contact alloc] init];

    NSNumber *idField = [NSNumber numberWithLongLong:sqlite3_column_int64(statement, idx++)];
    [contact setId:idField];

    NSString *nameField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, idx)];
    [contact setName:nameField];

    NSLog(@"id: %@", [contact id]);
    NSLog(@"name: %@", [contact name]);            

    [nameField release];

    [contactsList addObject:contact];
    [contact release];
}

sqlite3_finalize(statement);  
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜