我正在尝试从我的数据库“下面提到的代码”中删除一行,但它从不删除该行,并且似乎也从未调用过准备语句。
我可以插入并更新确定但是对于我的生活无法弄清楚这一点..
条形码是从可编辑的tableview传递给方法的字符串变量。
当我记录deletSQL字符串时,我可以看到正确的值但实际上从未删除记录。
有关详细信息,数据库中的条形码是主键。
数据库路径也都正确,并且调用正确的db文件。
只是删除功能无效...
-(void)delete:(NSString *)barcode{
const char *dbpath = [databasePath3 UTF8String];
if (sqlite3_open(dbpath, &database3) == SQLITE_OK)
{
NSLog(@"Opened OK");
NSString *deleteSQL = [NSString stringWithFormat: @"delete from assets where assetBarcode=\"%@\'",barcode];
NSLog(@"%@",deleteSQL);
const char *sql = [deleteSQL UTF8String];
if(sqlite3_prepare_v2(database3, sql, 1, &deleteStatement, NULL) == SQLITE_OK)
{
NSLog(@"SQL OK?");
if (sqlite3_step(deleteStatement) == SQLITE_ROW)
{
sqlite3_bind_text(deleteStatement, 1, [barcode UTF8String], -1, SQLITE_TRANSIENT);
}
}
else{
NSLog(@"WHAAAATTTTT");
}
}
sqlite3_step(deleteStatement);
sqlite3_reset(deleteStatement);
sqlite3_close(database3);
}
答案 0 :(得分:1)
数据库中的条形码列似乎是varchar
类型试试这个
- (void)delete:(NSString *)条形码
{{/ p>
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
if(deleteStatement == nil)
{
const char *sql = "DELETE FROM assets WHERE assetBarcode = ?";
if(sqlite3_prepare_v2(database, sql, -1, & deleteStatement, NULL) != SQLITE_OK)
NSAssert1(0, @"Error while creating delete statement. '%s'", sqlite3_errmsg(database));
}
sqlite3_bind_text(deleteStatement, 1, [barcode UTF8String], -1, SQLITE_TRANSIENT);
if(SQLITE_DONE != sqlite3_step(deleteStatement))
NSAssert1(0, @"Error while deleting data. '%s'", sqlite3_errmsg(database));
else
//SQLite provides a method to get the last primary key inserted by using sqlite3_last_insert_rowid
//noteID = sqlite3_last_insert_rowid(database);
//Reset the delete statement.
sqlite3_reset(deleteStatement);
sqlite3_close(database);
deleteStatement = nil;
}
else
sqlite3_close(database);
}
答案 1 :(得分:0)
关于SQL字符串......
delete from assets where assetBarcode=\"%@\'
参数正在使用报价打开,并以单引号关闭。两者都使用双引号或单引号。也许这与您的问题有关?