SQLite 支持“datareader”吗?

发布于 2024-11-02 01:00:57 字数 107 浏览 1 评论 0原文

我正在尝试在 SQLite 中使用数据读取器,但无法在我拥有的文档中找到任何内容(Kreibich 的“使用 SQLite”)。

有人可以告诉我它是否受支持以及在哪里可以找到一些示例?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最舍不得你 2024-11-09 01:00:57

是的,您只需要获取 System.Data.SQLite 即可。

它有两种变体,一种内置 SQLite,另一种要求您还提供单独的本机 sqlite DLL。

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.

樱花细雨 2024-11-09 01:00:57

sqlite api有一个逻辑上等同于.net阅读器的概念。这意味着,您发出查询,然后根据需要迭代读取数据。这会使内存保持较低水平,因为您没有将完整的结果集拉入内存。

首先,看一下其他包装器,例如 fmdb。

这是使用 iPhone 内部的 c api 的等效方法。您通过传递 sql 查询(sqlite 在幕后解析)来准备语句,然后调用相当于 .net reader read 方法的步骤。您可以像 .net 数据阅读器一样阅读列。请注意,此示例准备并完成(清理)。更有效的方法是保存准备好的语句,然后调用重置以避免让 sqlite 一遍又一遍地解析查询。

// 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);  

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);  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文