It's my first time trying to develop an SQLite database for IOS, or any IOS app for that matter. I'm trying to follow a tutorial I found online and adapt it for my own use. The database was created without any issues but my Insert statement never seems to return my error message. Nothing appears in the console as nothing drastic actually goes wrong with the program. If you need any more information I'll try my best to find it and update the question with it.
Here is my code:
// Method to store a GPS location
-(void)insertGPS:(GPS*)GPS
{
// Create insert statement for the person
NSString *insertStatement = [NSString stringWithFormat:@"INSERT INTO GPSJob (jobNo, sourceMonitor, positionNumber, latitude, longitude) VALUES (\"%@\", \"%@\", \"%@\",\"%@\",\"%@\")", GPS.jobNumber, GPS.sourceMonitor, GPS.positionNumber, GPS.latitude, GPS.longitude ];
// Define an error
char *error;
// Attempt to execute the insert statement
if ( sqlite3_exec(databaseHandle, [insertStatement UTF8String], NULL, NULL, &error) == SQLITE_OK)
{
NSLog(@"GPS inserted into database with values: %@, %@, %@, %@, %@.", GPS.jobNumber, GPS.sourceMonitor, GPS.positionNumber, GPS.latitude, GPS.longitude);
}
// If the insert statement is not okay
else {
NSLog(@"Error: %s", sqlite3_errmsg(databaseHandle));
}
}
Here is the tutorial I'm following: http://www.apptite.be/tutorial_ios_sqlite.php
The updated error message said this :
2015-08-12 15:28:25.299 NoiseApp[7602:207] Error: out of memory
-----------------------------------Solution----------------------------------
For anyone who wants the solution to this problem I simply amended the function to read as follows :
// Method to store a GPS location
-(void)insertGPS:(GPS*)GPS
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:@"GPS.db"];
if(sqlite3_open([databasePath UTF8String], &databaseHandle) ==SQLITE_OK){
// Create insert statement for the person
NSString *insertStatement = [NSString stringWithFormat:@"INSERT INTO GPSJob (jobNo, sourceMonitor, positionNumber, latitude, longitude) VALUES (\"%@\", \"%@\", \"%@\",\"%@\",\"%@\")", GPS.jobNumber, GPS.sourceMonitor, GPS.positionNumber, GPS.latitude, GPS.longitude ];
// Define an error
char *error;
// Attempt to execute the insert statement
if ( sqlite3_exec(databaseHandle, [insertStatement UTF8String], NULL, NULL, &error) == SQLITE_OK)
{
NSLog(@"GPS inserted into database with values: %@, %@, %@, %@, %@.", GPS.jobNumber, GPS.sourceMonitor, GPS.positionNumber, GPS.latitude, GPS.longitude);
}
// If the insert statement is not okay
else {
NSLog(@"Error: %s", sqlite3_errmsg(databaseHandle));
}
}
}
databaseHandle is not defined in your function. You should get your reference to the sqlite3_open somewhere.
Use sqlite3_open to open your database, if you didn't do this already. Example of opening. (note if you want to write to this database you should copy it from the resourcePath to somewhere writeable, and open this version)