【发布时间】:2012-12-31 19:36:19
【问题描述】:
在我的应用程序中,我将一些数据保存在本地 Sqlite 数据库中。 Sqlite 打开,插入一切正常。但是当我尝试更新特定行/记录的数据时,如果失败并显示 ASSERTION FAILURE 消息..
* -[gss_databaseHandler updateRecord::::] 中的断言失败,/Users/gss/Desktop/SalesPro copy/SalesPro/../gss_databaseHandler.m:210
NSString *databasePath;
// Method to open a database, the database will be created if it doesn't exist
-(void)initDatabase
{
// Create a string containing the full path to the sqlite.db inside the documents folder
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
databasePath = [documentsDirectory stringByAppendingPathComponent:@"contact1"];
// Check to see if the database file already exists
bool databaseAlreadyExists = [[NSFileManager defaultManager] fileExistsAtPath:databasePath];
// Open the database and store the handle as a data member
if (sqlite3_open([databasePath UTF8String], &databaseHandle) == SQLITE_OK)
{
// Create the database if it doesn't yet exists in the file system
if (!databaseAlreadyExists)
{
// Create the contactList table
const char *sqlStatement = "CREATE TABLE IF NOT EXISTS contactList (sapCustId TEXT, sapContactId TEXT, record_ID NUMERIC PRIMARY KEY, timestamp TEXT)";
char *error;
if (sqlite3_exec(databaseHandle, sqlStatement, NULL, NULL, &error) == SQLITE_OK)
{
NSLog(@"Database and tables created.");
}
else
{
NSLog(@"Error: in creating/opening database");
}
}
}
}
- (void) updateRecord:(int)recordID:(NSString *)sapCustId:(NSString *)sapContactId:(NSString *)timestamp {
[self initDatabase];
sqlite3_stmt *updateStmt = nil;
if(updateStmt == nil) {
const char *sql_stmt = "update contactList Set sapCustId = ?, sapContactId = ?, timestamp = ? Where record_ID = ?";
if(sqlite3_prepare_v2(databaseHandle, sql_stmt, -1, &updateStmt, NULL) != SQLITE_OK)
NSAssert1(0, @"Error while creating update statement. '%s'", sqlite3_errmsg(databaseHandle));
}
sqlite3_bind_text(updateStmt, 0, [sapCustId UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(updateStmt, 1, [sapContactId UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_int(updateStmt, 2, recordID);
sqlite3_bind_text(updateStmt, 3, [timestamp UTF8String], -1, SQLITE_TRANSIENT);
if(SQLITE_DONE != sqlite3_step(updateStmt))
NSAssert1(0, @"Error while updating. '%s'", sqlite3_errmsg(databaseHandle));
sqlite3_finalize(updateStmt);
sqlite3_close(databaseHandle);
//Reclaim all memory here.
[sapContactId release];
[sapCustId release];
}
请帮忙!..
【问题讨论】:
-
@Lefteris 用错误消息更新它!
-
从您的 sql 更新语句中,我看到您的 recordID 应该是第 4 个而不是第 3 个传递给 tte sqlite_bind 的变量
-
@Lefteris 我相应地更改了它.. 但收到相同的错误。
-
@ibiren 您已经竭尽全力记录
sqlite3_errmsg,这是错误的关键部分,但您没有与我们分享。这就是我们需要的部分。断言发生的事实并不像sqlite3_errmsg那样有趣,这将告诉我们原因。它隐藏在控制台日志中,因此您只需查看一下即可。就您的错误而言,lefteris 关于参数的顺序是正确的,而 Prateek 关于sqlite3_reset是不正确的(尽管他认为它是基于 1 的索引是正确的)。
标签: ios sqlite sql-update xcode4.5