我正在从数据库中删除记录。它工作正常。但是当数据库中没有数据时它会崩溃。代码在下面给出
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];
以上是代码
答案 0 :(得分:1)
首先使用以下语句检查数据库是否为空
if(sqlite3_column_text(statement, 0) != nil){//Data exists}
然后在if方法中添加代码以删除dataBase。 它工作正常。
答案 1 :(得分:0)
query = [NSString stringWithFormat:@"delete from %@ where %@",query];
这个查询意味着什么我认为你的查询错误请仔细检查
如果错误以编程方式持续检查
使用if(sqlite3_column_text(statement, 0) != nil)
答案 2 :(得分:0)
if (condition for check database empty (select * from table)){
}else{
//Perform Delete operation
}
答案 3 :(得分:0)
我发现有一个问题是,无论状态为sqlite3_finalize(statement);
,您都在调用sqlite3_prepare_v2
。如果sqlite3_finalize
成功执行,您只需致电sqlite3_prepare_v2
。
将其更改为:
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);
}