2

我对iphone开发很陌生。

无论如何,我现在已经搜索了 1.5 小时以找到添加 UISearchBar 的可能性/教程或任何内容(它已添加到 RootViewController.XIB)。有很多 Tutos 描述了如何将 Array 连接到 UISearchBar。我想做的是将我已经在工作的 SQLite 数据库连接到 UISearchBar,这样我就可以进行 FullTextSearch。

就像.. 假设我的 UITableView 中有 100 个来自 SQLite 数据库的条目,当我输入“abc”字符串时,应该只显示包括 abc 在内的相关条目。

有任何想法吗?

非常感谢您!

4

1 回答 1

5

您的 UISearchBarDelegate 类可以建立到数据库的连接并在某处运行查询:

NSString *sql = @"SELECT author FROM books WHERE title like ?||'%'";
sqlite3_stmt *stmt;

if (sqlite3_prepare_v2(database,
   [sql cStringUsingEncoding:NSUTF8StringEncoding],
   -1, &stmt, NULL) == SQLITE_OK) {

   NSString *title = @"programming";
   sqlite3_bind_text(stmt, 1, [title UTF8String], -1, SQLITE_TRANSIENT);


   while (sqlite3_step(stmt) == SQLITE_ROW) {
      NSString *author = sqlite3_column_string(stmt, 0);
      NSLog(@"Add to presented authors: %@", author);
   }

}

您需要在查询之前建立与数据库的连接。

这里有一个很好的教程

于 2009-12-11T23:50:10.417 回答