Use UISeachBar with SQLite3
-
19-09-2019 - |
Question
I'm pretty new to iphone development.
Anyhow I've searched now for 1,5 hrs to find a possibility/tutorial or whatsoever to add a UISearchBar (it's already added to the RootViewController.XIB). There are a lot of Tutos describing how to connect an Array to a UISearchBar. What I would like to do is connect my already working SQLite Database to the UISearchBar, so that I'm able to do a FullTextSearch.
Like.. let's say I have 100 entries within my UITableView out of the SQLite Database and when I enter a "abc" string then only the relevant entries including abc should be shown.
Any ideas?
Thank you very much in advance!
Solution
Your UISearchBarDelegate class could establish a connection to the database and run a query somewhere along the lines of:
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);
}
}
You need to establish a connection to the database before querying it.
There is a nice tutorial here