Strange SQLite3 error: can't find column
I'm stuck on this problem here. I've got an application which reads out an XML-file. This information is stored correctly into a NSMutableDictionary.
The next thing I want to do, is to add all the elements from that NSMutableDictionary into a database using SQLite3.
In the app delegate, I'm creating the database using:
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
// Bu开发者_C百科ild the path to the database file
databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"incidents.db"]];
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: databasePath ] == NO)
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt = "CREATE TABLE IF NOT EXISTS INCIDENTS (ID INTEGER PRIMARY KEY, SERIAL TEXT, CI TEXT, FAMILY TEXT, DEVICEDESCRIPTION TEXT, LOCATION TEXT, SUBLOCATION TEXT, UPDOWN TEXT, PROBLEMDESCRIPTION TEXT, ADDINFO TEXT, PROBLEMLOCATION TEXT, PROBLEMCATEGORY TEXT, CONTFIRSTNAME TEXT, CONTLASTNAME TEXT, CONTSALUTATION TEXT, CONTPHONENUMBER TEXT, LOCALCALL TEXT, CICATEGORY TEXT, STATUS TEXT)";
if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
{
NSLog(@"Failed to create table");
}
sqlite3_close(contactDB);
} else {
NSLog(@"Failed to open/create database");
}
}
[filemgr release];
In an other class, I'm using the code:
NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO INCIDENTS (ID, SERIAL, CI, FAMILY, DEVICEDESCRIPTION, LOCATION, SUBLOCATION, UPDOWN, PROBLEMDESCRIPTION, ADDINFO, PROBLEMLOCATION, PROBLEMCATEGORY, CONTFIRSTNAME, CONTLASTNAME, CONTSALUTATION, CONTPHONENUMBER, LOCALCALL, CICATEGORY, STATUS) VALUES (\"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\")", [item objectForKey:@"ID"], [item objectForKey:@"Serial"], [item objectForKey:@"ConfigurationAlias"], [item objectForKey:@"Family"], [item objectForKey:@"DeviceDescription"], [item objectForKey:@"Location"], [item objectForKey:@"SubLocation"], [item objectForKey:@"UpDownIndicator"], [item objectForKey:@"ProblemDescription"], [item objectForKey:@"AdditionalInformation"], [item objectForKey:@"ProblemLocation"], [item objectForKey:@"ProblemCategory"], [item objectForKey:@"ContactPersonFirstName"], [item objectForKey:@"ContactPersonLastName"], [item objectForKey:@"ContactPersonSalutation"], [item objectForKey:@"ContactPersonPhoneNumber"], [item objectForKey:@"LocalCallNumber"], [item objectForKey:@"ConfigurationItemCategory"], [item objectForKey:@"Status"]];
const char *insert_stmt = [insertSQL UTF8String];
//NSLog(@"could not prepare statement: %s\n", sqlite3_errmsg(contactDB));
sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL);
if(sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL) != SQLITE_DONE){
NSLog(@"Error: Failed to prepare stmt with message '%s'", sqlite3_errmsg(contactDB));
}
if (sqlite3_step(statement) == SQLITE_DONE)
{
NSLog(@"Succesfully added in DB");
} else {
NSLog(@"Failed to add incident in DB");
}
sqlite3_finalize(statement);
sqlite3_close(contactDB);
The error I'm getting using the sqlite3_errmsg statement, is:
2011-05-09 10:04:12.029 Fleet Manager[1053:207] Error: Failed to prepare stmt with message 'table INCIDENTS has no column named PROBLEMDESCRIPTION'
Any idea whats going on?
EDIT: It works when I remove the Problemdescription-part out of the adding. Still a problem, since it leaves me with an empty column...
From the error message, it is clear that the table in your SQLite3 database does not have a PROBLEMDESCRIPTION column. I would speculate that, at some point, your create statement had a spelling mistake in it that has not been corrected because you don't recreate the table if it already exists.
To verify this, open the database with the sqlite3 command line tool and run the .schema INCIDENTS
command.
On an unrelated note, it is a very bad idea to insert row data using -stringWithFormat:
You should always use sqlite_bind*() to avoid the hassles of having to escape reserved characters.
精彩评论