我正在创建一个使用Sqlite来保存数据的iPhone应用程序,而我插入/选择操作代码正常,但对于更新/删除操作代码失败即可。 我发现一些链接n stacker flow reg相同而且有些如何我修复了SQLITE_BUSY错误使用BEGIN Transaction和Commit语句包围我的更新/删除语句。现在如果我执行更新/删除语句,它返回< strong> SQLITE_OK:0:成功的结果。也在这里嵌入代码。我请开发人员告诉我哪里出错了。 对于测试目的,我硬编码了像 &#34;更新药物药物剂量=&#39; 2&#39;其中drugid = 1&#34;。 在exe语句之后它返回SQLITE_OK,但如果我检查数据库,则值不会更新。 我接着介绍了两种方法。
(BOOL )updateDrugDetails:(DrugDetails *)drugDetails{
BOOL *status=TRUE;
NSString *databasePath;
sqlite3 *database ;
NSString *docsDir;
NSArray *dirPaths;
sqlite3_stmt *updateStatement;
BOOL statusUpdate;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
databasePath = [[NSString alloc] initWithString:
[docsDir stringByAppendingPathComponent: @"drugdb2.sqlite"]];
BOOL isSuccess = YES;
NSFileManager *filemgr = [NSFileManager defaultManager];
NSLog(@"databasePath %@",databasePath);
if ([filemgr fileExistsAtPath: databasePath ] == NO)
{
NSLog(@"Failed to open/create database");
isSuccess = NO;
}
else
{
isSuccess = YES;
NSLog(@"Able to open/create database");
}
if(isSuccess)
{
NSInteger drugID= [drugDetails drugId];
NSString *drugDose= [drugDetails drugDose];
NSString *drugStartDate= [drugDetails drugStartDate];
NSString *drugEndDate= [drugDetails drugEndDate];
const char *dbpath = [databasePath UTF8String];
NSString *temp=[NSString stringWithFormat:@"%d", drugID];
int tempInt=[temp integerValue];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
/*
// First Approach
*updateSQL =
[NSString stringWithFormat:
@"update drug set drugdose=?,drugstartdate=? ,drugenddate=?
where drugid =?"];
const char *update_stmt = [updateSQL UTF8String];
const char *drgDose = [drugDose UTF8String];
const char *drgStartDate = [drugStartDate UTF8String];
const char *drgEndDate= [drugEndDate UTF8String];
sqlite3_prepare_v2(database, update_stmt,-1, &updateStatement, NULL);
// sqlite3_bind_text(updateStmt, 4, [name.text UTF8String],
-1, SQLITE_TRANSIENT);
sqlite3_bind_text(updateStatement, 1, drgDose,-1,SQLITE_TRANSIENT);
sqlite3_bind_text(updateStatement, 2, drgStartDate,-1,SQLITE_TRANSIENT);
sqlite3_bind_text(updateStatement, 3, drgEndDate,-1,SQLITE_TRANSIENT);
sqlite3_bind_int(updateStatement, 4, tempInt);
sqlite3_busy_timeout(database, -1);
int updtStatus;
updtStatus=sqlite3_step(updateStatement);
if (updtStatus == SQLITE_DONE)
{
NSLog(@"Drug Details Updated with Status and rem Count");
statusUpdate= YES;
}else
{
NSLog(@"Failed to Update Drug Deatils with Status
rem Count%d",updtStatus);
statusUpdate= NO;
}
if(statusUpdate==TRUE)
{
NSLog(@"drug updated");
status=TRUE;
// sqlite3_reset(updateStatement);
sqlite3_finalize(updateStatement);
sqlite3_close(database);
database=nil;
databasePath=nil;
updateStatement=nil;
databasePath =nil;
}
else
{
NSLog(@"Error: %d", statusUpdate);
status=FALSE;
// sqlite3_reset(updateStatement);
sqlite3_finalize(updateStatement);
sqlite3_close(database);
database=nil;
databasePath=nil;
updateStatement=nil;
databasePath =nil;
}
sqlite3_finalize(updateStatement);
sqlite3_close(database);
*/
//第二种方法,尽管为了测试目的而硬编码值,但它没有在数据库中更新//。
NSString *replaceStatement =
[NSString stringWithFormat:@"update drug set
drugdose='2',drugstartdate='10/10/10',drugenddate='10/10/10' where drugid=1"];
char *error;
int updateStatus;
// sqlite3_busy_timeout(database, -1);
NSString *begTxn=@"BEGIN TRANSACTION;";
NSString *commit=@"COMMIT;";
sqlite3_exec(database,[begTxn UTF8String],NULL,NULL,&error);
updateStatus= sqlite3_exec(database,
[replaceStatement UTF8String], NULL, NULL, &error);
// Here the updatStatus returns 0 ,i.e sqlite Success Operatiion
sqlite3_exec(database,[commit UTF8String],NULL,NULL,&error);
if ((updateStatus == 0))
{
NSLog(@"Drug Details updated");
sqlite3_close(database);
}
else
{
NSLog(@"Error while Updating Drug Details %s", error);
sqlite3_close(database);
}
}
else
{
NSLog(@"Failed to Open Dtabase");
status=FALSE;
}
}else
{
NSLog(@"Failed to get the DB Path");
status=FALSE;
}
return status;
}