gpt4 book ai didi

ios - 从sqlite中的空数据库中删除

转载 作者:行者123 更新时间:2023-11-29 03:22:42 26 4
gpt4 key购买 nike

我正在从数据库中删除记录。它工作正常。但是当数据库中没有数据时它会崩溃。代码如下

 int noOfRecordsDeleted;

[self openDatabaseConnection];

// query = [NSString stringWithFormat:@"delete from %@ where %@",query];
NSLog(@"del query=%@",query);
const char *sql = [query cStringUsingEncoding:NSUTF8StringEncoding];
sqlite3_stmt *statement = nil;

if(sqlite3_prepare_v2(dataBaseConnection,sql, -1, &statement, NULL)!= SQLITE_OK)
{
NSAssert1(0,@"error preparing statement",sqlite3_errmsg(dataBaseConnection));
}



else
{
int success = sqlite3_step(statement);
NSLog(@"%d",success);
}
sqlite3_finalize(statement);
noOfRecordsDeleted = sqlite3_changes(dataBaseConnection);
[self closeDatabaseConnection];
return noOfRecordsDeleted;

工作正常。但是如果我添加数据我清空数据库它就会崩溃

[self openDatabaseConnection];

NSString *query;

query = [NSString stringWithFormat:@"insert into Userdetail(aboutMe,anniversary,areacode,birthdate,device_id,chat_id,emailid,gender ,image,last_login,latitute,longitude,Looking_For,mobilenumber,mobilenumber1 ,mobilenumber2,mood,name,password,place,profileviews,statusmessage) values ('%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@')",aboutMe,anniversarydate,areacode,birthdate,
device_id,chat_id,email_id,gender,image,last_login,latitude,longitude,looking_for,mobilenumber,mobilenumber1,mobilenumber2,mood,name,password,place,profileviews,statusmessage];

NSLog(@"saveNewTemplateData query=%@",query);
const char *sql = [query cStringUsingEncoding:NSUTF8StringEncoding];
sqlite3_stmt *statement = nil;if(sqlite3_prepare_v2(dataBaseConnection,sql , -1, &statement, NULL)!= SQLITE_OK)
{
NSAssert1(0,@"error preparing statement",sqlite3_errmsg(dataBaseConnection));
}
else
{
sqlite3_step(statement);
}
sqlite3_finalize(statement);
[self closeDatabaseConnection];

上面是代码

最佳答案

首先使用以下语句检查数据库是否为空

if(sqlite3_column_text(statement, 0) != nil){//Data exists}

然后在if方法中添加删除数据库的代码。它工作正常。

关于ios - 从sqlite中的空数据库中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20882930/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com