奇怪的SQLite3错误:找不到列

时间:2011-05-09 08:24:48

标签: sql objective-c ios4 sqlite

我在这里遇到了这个问题。我有一个读取XML文件的应用程序。此信息正确存储在NSMutableDictionary中。

我要做的下一件事是使用SQLite3将NSMutableDictionary中的所有元素添加到数据库中。

在app委托中,我使用以下方法创建数据库:

NSString *docsDir;
NSArray *dirPaths;

// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

docsDir = [dirPaths objectAtIndex:0];

// Build 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];

在另一堂课中,我正在使用代码:

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);

我使用sqlite3_errmsg语句得到的错误是: 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'

有什么想法吗?

编辑:当我从添加中删除问题描述部分时,它可以正常工作。仍然是一个问题,因为它留下了一个空列...

1 个答案:

答案 0 :(得分:4)

从错误消息中可以清楚地看出,SQLite3数据库中的表没有PROBLEMDESCRIPTION列。我推测,在某些时候,你的create语句中有一个拼写错误,但没有得到纠正,因为如果表已经存在,你就不会重新创建表。

要验证这一点,请使用sqlite3命令行工具打开数据库并运行.schema INCIDENTS命令。


在一个不相关的注释中,使用-stringWithFormat:插入行数据是very bad idea您应该始终使用sqlite_bind*()来避免必须逃避保留字符的麻烦。