So my code is suppose to create a database and table, and then store my location data in the table. It’s hitting all the right logs that I made, like “Created Table” and “Location inserted”, but when I go to find the file it’s either (a) not there or (b) there but has no location data in it.
I started off using CoreData, but it quickly turned into a nightmare so I changed to using sqlite directly.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
locationManager =[[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = 10;
[locationManager startUpdatingLocation];
// Create a string containing the full path to the bold2.db inside the documents folder
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:@"bold2.sql"];
// Check to see if the database file already exists
bool databaseAlreadyExists = [[NSFileManager defaultManager] fileExistsAtPath:databasePath];
// Open the database and store the handle as a data member
if (sqlite3_open([databasePath UTF8String], &databasehandle) == SQLITE_OK)
{
// Create the database if it doesn't yet exists in the file system
if (!databaseAlreadyExists)
{
// Create the LOCATION table
const char *sqlStatement = "CREATE TABLE IF NOT EXISTS LOCATION (ID INTEGER PRIMARY KEY AUTOINCREMENT, latitude DOUBLE, longitude DOUBLE, timeStamp DATE)";
char *error;
if (sqlite3_exec(databasehandle, sqlStatement, NULL, NULL, &error) == SQLITE_OK)
{
NSLog(@"Created Table");
}
}
Here is the location delegate where I’m getting a new location and inserting it into the database. The weird thing is that it actually hits the “Location inserted” log.
-(void) locationManager: (CLLocationManager *) manager
didUpdateToLocation: (CLLocation *) newLocation
fromLocation: (CLLocation *) oldLocation
{
NSString *insertStatement = [NSString stringWithFormat:@"INSERT INTO location (latitude, longitude, timeStamp) VALUES (%g, %g, %g)",newLocation.coordinate.latitude, newLocation.coordinate.longitude, newLocation.timestamp];
char *error;
if ( sqlite3_exec(databasehandle, [insertStatement UTF8String], NULL, NULL, &error) == SQLITE_OK)
{
NSLog(@"Location inserted. %g", newLocation.coordinate.latitude);
}
else NSLog(@"Error: %s", error);
}
I solved it. Since I’m remoting into a minimac the file path to the db was hidden. I just pasted the correct filepath in go to folder and found the right file.