奇怪的 SQLite3 错误:找不到列

发布于 2024-11-05 13:19:27 字数 3571 浏览 0 评论 0原文

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

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

在应用程序委托中,我使用以下命令创建数据库:

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] 错误:无法使用消息“表 INCIDENTS 没有名为 PROBLEMDESCRIPTION 的列”准备 stmt

知道发生了什么吗?

编辑:当我从添加中删除问题描述部分时,它会起作用。仍然是一个问题,因为它给我留下了一个空列......

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

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

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

硬不硬你别怂 2024-11-12 13:19:27

从错误消息中可以清楚地看出,您的 SQLite3 数据库中的表没有 PROBLEMDESCRIPTION 列。我推测,在某些时候,您的创建语句中存在拼写错误,该错误尚未更正,因为如果表已经存在,您不会重新创建该表。

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


在一个不相关的说明中,使用 -stringWithFormat:< 插入行数据是非常糟糕的主意 /code> 您应该始终使用 sqlite_bind*() 以避免必须转义的麻烦保留字符。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文