1

我在 iPad 开发项目中写入 SQLite 数据库时遇到问题。这是我使用插入语句写入数据库的代码。当我执行语句时,Sqlite 没有发回错误,但数据仍然没有保存在数据库中。

我的插入语句的简化版本:

//Insert Into POHeader(CustID, CatalogName) Values('0', 'webdemo')


@try {
        NSFileManager *fileMgr = [NSFileManager defaultManager];
        NSString *dbPath = [[[NSBundle mainBundle] resourcePath]stringByAppendingPathComponent:@"Orders.db"];
        BOOL success = [fileMgr fileExistsAtPath:dbPath];
        if(!success)
        {
            NSLog(@"Cannot locate database file '%@'.", dbPath);
        }
        if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
        {
            NSLog(@"An error has occured: %@", sqlite3_errmsg(db));
        }

    sql = [self prepareSqlForPOHeader];
    sqlite3_stmt *sqlStatement;

    int sqlResult = (sqlite3_prepare_v2(db, sql, -1, &sqlStatement, NULL));

    if (sqlResult != SQLITE_OK)// (sqlite3_prepare_v2(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
    {
        NSLog(@"Problem with prepare statement:  %@", sqlite3_errmsg(db));
    }else{
        //success
    }

    sqlite3_finalize(sqlStatement);

}
@catch (NSException *exception) {
    NSLog(@"Problem with prepare statement: %@", sqlite3_errmsg(db));
}
@finally {
    sqlite3_close(db);
}
4

2 回答 2

0

执行语句,这将起作用

于 2012-08-10T16:43:29.503 回答
0

你忘了添加这一行:sqlite3_step(sqlStatement);

将代码更改为:

if ((sqlite3_prepare_v2(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
{
    NSLog(@"Problem with prepare statement:  %@", sqlite3_errmsg(db));
}
else
{
    sqlite3_step(sqlStatement);
    sqlite3_finalize(sqlStatement);
}
于 2012-08-10T17:45:19.210 回答