Skip to content

Instantly share code, notes, and snippets.

@rd13
Last active December 17, 2015 11:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rd13/5605840 to your computer and use it in GitHub Desktop.
Save rd13/5605840 to your computer and use it in GitHub Desktop.
#import "Bikes.h"
static sqlite3 *database = nil;
static sqlite3_stmt *detailStmt = nil;
@implementation Bikes
@synthesize BikesID, BikesName, price, isDirty, modelCount;
@synthesize modelName,modelID,modelType,groupCount,modelBrand;
@synthesize year,category,displacement,power,topSpeed,fuelConsumption,powerWeightRatio,naughtSixty,torque;
+ (void) getInitialDataToDisplay:(NSString *)dbPath {
SQLAppDelegate *appDelegate = (SQLAppDelegate *)[[UIApplication sharedApplication] delegate];
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
if(sqlite3_extension_init(database)){
NSLog(@"extension loaded ok");
}
const char *sql = "select b.id, b.name, count(m.brand) from motorcycles m join brands b on b.id = m.brand group by m.brand";
sqlite3_stmt *selectstmt;
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
while(sqlite3_step(selectstmt) == SQLITE_ROW) {
NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);
NSInteger modelCount = sqlite3_column_int(selectstmt, 2);
Bikes *BikesObj = [[Bikes alloc] initWithPrimaryKey:primaryKey];
BikesObj.BikesName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
BikesObj.modelCount = modelCount;
BikesObj.modelID = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)];
BikesObj.isDirty = NO;
[appDelegate.BikesArray addObject:BikesObj];
[BikesObj release];
}
}
}
else
sqlite3_close(database);
}
+ (int) getModelFromReg:(NSString *)str:(NSString *)mdl:(NSString *)yr {
SQLAppDelegate *appDelegate = (SQLAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.detailArray removeAllObjects];
const char *sql = [[NSString stringWithFormat:@"select m.id, levenshtein('%@',m.model) as levenshtein, abs(%@-m.year) as yearsaway from motorcycles m join brands b on b.id = m.brand where lower(b.name) = lower('%@') ORDER BY levenshtein,yearsaway limit 1",mdl,yr,str] UTF8String];
NSLog(@"Brand: %@ Model: %@ Year: %@",str,mdl,yr);
if(sqlite3_prepare_v2(database, sql, -1, &detailStmt, NULL) != SQLITE_OK)
NSAssert1(0, @"Error while creating detail view statement. '%s'", sqlite3_errmsg(database));
int returnModelId;
while(sqlite3_step(detailStmt) == SQLITE_ROW) {
returnModelId = sqlite3_column_int(detailStmt, 0);
}
sqlite3_reset(detailStmt);
return returnModelId;
}
+ (void) finalizeStatements {
if(database) sqlite3_close(database);
}
- (id) initWithPrimaryKey:(NSInteger) pk {
[super init];
BikesID = pk;
return self;
}
@end
#import "SQLAppDelegate.h"
@implementation SQLAppDelegate
- (void)applicationDidFinishLaunching:(UIApplication *)application {
//Copy database to the user's phone if needed.
[self copyDatabaseIfNeeded];
CoverViewController *thecoverViewController = [[CoverViewController alloc] initWithNibName:@"CoverViewController" bundle:nil];
[window addSubview:thecoverViewController.view];
[Bikes getInitialDataToDisplay:[self getDBPath]];
[window makeKeyAndVisible];
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Save data if appropriate
[Bikes finalizeStatements];
}
- (void) copyDatabaseIfNeeded {
//Using NSFileManager we can perform many file system operations.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *dbPath = [self getDBPath];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if(!success) {
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"SQL.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
NSLog(@"Database file copied from bundle to %@", dbPath);
if (!success)
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
} else {
NSLog(@"Database file found at path %@", dbPath);
}
}
- (NSString *) getDBPath {
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSURL *pathURL= [NSURL fileURLWithPath:documentPath];
[self addSkipBackupAttributeToItemAtURL:pathURL];
return [documentPath stringByAppendingPathComponent:@"SQL.sqlite"];
}
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
if (&NSURLIsExcludedFromBackupKey == nil) { // iOS <= 5.0.1
const char* filePath = [[URL path] fileSystemRepresentation];
const char* attrName = "com.apple.MobileBackup";
u_int8_t attrValue = 1;
int result = setxattr(filePath, attrName, &attrValue, sizeof(attrValue), 0, 0);
return result == 0;
} else { // iOS >= 5.1
NSError *error = nil;
[URL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:&error];
return error == nil;
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment