Skip to content

Instantly share code, notes, and snippets.

@igaiga
Created September 18, 2011 02:30
Show Gist options
  • Save igaiga/1224631 to your computer and use it in GitHub Desktop.
Save igaiga/1224631 to your computer and use it in GitHub Desktop.
iOS sqlite select statement sample
//#import <sqlite3.h>
sqlite3* d = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent: @"testdb.db"];
if (sqlite3_open([path UTF8String], &d) != SQLITE_OK) {
// Even though the open failed, call close to properly clean up resources.
sqlite3_close(d);
NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(d));
// Additional error handling, as appropriate...
}
sqlite3_stmt *sqlstmt;
char *sql = "select * from CartaReadingSystemMaster;";
int ret;
ret = sqlite3_prepare_v2(d, sql, -1, &sqlstmt, NULL);
if (ret == SQLITE_OK) {
NSLog(@"*** SQLITE_OK");
if(SQLITE_ROW == sqlite3_step(sqlstmt)){
NSLog(@"%s",sqlite3_column_text(sqlstmt,0));
NSLog(@"%s",sqlite3_column_text(sqlstmt,1));
NSLog(@"%s",sqlite3_column_text(sqlstmt,2));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment