Last active
February 6, 2017 14:02
-
-
Save neerajgoel82/9aff51edb9b8e5168d78 to your computer and use it in GitHub Desktop.
This is the gist of using sqlite with ios app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FailedBankInfo.h | |
---------------- | |
@interface FailedBankInfo : NSObject { | |
int _uniqueId; | |
NSString *_name; | |
NSString *_city; | |
NSString *_state; | |
} | |
@property (nonatomic, assign) int uniqueId; | |
@property (nonatomic, copy) NSString *name; | |
@property (nonatomic, copy) NSString *city; | |
@property (nonatomic, copy) NSString *state; | |
- (id)initWithUniqueId:(int)uniqueId name:(NSString *)name city:(NSString *)city | |
state:(NSString *)state; | |
@end | |
FailedBankInfo.m | |
---------------- | |
@implementation FailedBankInfo | |
@synthesize uniqueId = _uniqueId; | |
@synthesize name = _name; | |
@synthesize city = _city; | |
@synthesize state = _state; | |
- (id)initWithUniqueId:(int)uniqueId name:(NSString *)name city:(NSString *)city | |
state:(NSString *)state { | |
if ((self = [super init])) { | |
self.uniqueId = uniqueId; | |
self.name = name; | |
self.city = city; | |
self.state = state; | |
} | |
return self; | |
} | |
- (void) dealloc { | |
self.name = nil; | |
self.city = nil; | |
self.state = nil; | |
} | |
@end | |
FailedBankDatabase.h | |
-------------------- | |
#import <Foundation/Foundation.h> | |
#import <sqlite3.h> | |
@interface FailedBankDatabase : NSObject { | |
sqlite3 *_database; | |
} | |
+ (FailedBankDatabase*)database; | |
- (NSArray *)failedBankInfos; | |
FailedBankDatabase.m | |
-------------------- | |
static FailedBankDatabase *_database; | |
+ (FailedBankDatabase*)database { | |
if (_database == nil) { | |
_database = [[FailedBankDatabase alloc] init]; | |
} | |
return _database; | |
} | |
- (id)init { | |
if ((self = [super init])) { | |
NSString *sqLiteDb = [[NSBundle mainBundle] pathForResource:@"banklist" | |
ofType:@"sqlite3"]; | |
if (sqlite3_open([sqLiteDb UTF8String], &_database) != SQLITE_OK) { | |
NSLog(@"Failed to open database!"); | |
} | |
} | |
return self; | |
} | |
- (void)dealloc { | |
sqlite3_close(_database); | |
} | |
- (NSArray *)failedBankInfos { | |
NSMutableArray *retval = [[NSMutableArray alloc] init]; | |
NSString *query = @"SELECT id, name, city, state FROM failed_banks ORDER BY close_date DESC"; | |
sqlite3_stmt *statement; | |
if (sqlite3_prepare_v2(_database, [query UTF8String], -1, &statement, nil) | |
== SQLITE_OK) { | |
while (sqlite3_step(statement) == SQLITE_ROW) { | |
int uniqueId = sqlite3_column_int(statement, 0); | |
char *nameChars = (char *) sqlite3_column_text(statement, 1); | |
char *cityChars = (char *) sqlite3_column_text(statement, 2); | |
char *stateChars = (char *) sqlite3_column_text(statement, 3); | |
NSString *name = [[NSString alloc] initWithUTF8String:nameChars]; | |
NSString *city = [[NSString alloc] initWithUTF8String:cityChars]; | |
NSString *state = [[NSString alloc] initWithUTF8String:stateChars]; | |
FailedBankInfo *info = [[FailedBankInfo alloc] | |
initWithUniqueId:uniqueId name:name city:city state:state]; | |
[retval addObject:info]; | |
} | |
sqlite3_finalize(statement); | |
} | |
return retval; | |
} | |
Final usage | |
----------- | |
NSArray *failedBankInfos = [FailedBankDatabase database].failedBankInfos; | |
for (FailedBankInfo *info in failedBankInfos) { | |
NSLog(@"%d: %@, %@, %@", info.uniqueId, info.name, info.city, info.state); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment