简体   繁体   English

从SQLite的空数据库中删除

[英]Delete from empty database in sqlite

I am deleting records from the database.and it is working fine .But it is crashing when there is no data in the database. 我正在从数据库中删除记录。它工作正常。但是,当数据库中没有数据时,它会崩溃。 The code is given Below 代码如下

 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;

It is working fine. 一切正常。 But if i am adding data i empty database it is crashing 但是,如果我要添加数据,则我的数据库为空,这将导致崩溃

[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];

above is the code 以上是代码

First of all check the Database is empty or not by using the following statement 首先使用以下语句检查数据库是否为空

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

Then in the if method add the code to delete the dataBase. 然后在if方法中添加代码以删除数据库。 It works fine. 工作正常。

query = [NSString stringWithFormat:@"delete from %@ where %@",query];

what does this query means I think its the mistake of your query please doublecheck it 这个查询是什么意思,我认为这是您查询的错误,请仔细检查

if the error persists programmatically check by using if(sqlite3_column_text(statement, 0) != nil) 如果错误仍然存​​在,请使用if(sqlite3_column_text(statement, 0) != nil)编程方式检查

if (condition for check database empty (select * from table)){

}else{

//Perform Delete operation

}

One issue I saw that you are calling sqlite3_finalize(statement); 我看到的一个问题是您正在调用sqlite3_finalize(statement); regardless the state of sqlite3_prepare_v2 . 无论sqlite3_prepare_v2的状态如何。 You only need to call sqlite3_finalize if the sqlite3_prepare_v2 executed successfully. 如果sqlite3_prepare_v2成功执行,则仅需要调用sqlite3_finalize

Change that to: 更改为:

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM