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

标签 ios iphone sqlite

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

 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/

相关文章:

mysql - "sqlite_see_if_its_a_number"与 "mysql_bind_type_guessing"有什么关系?

sqlite - 如何在group_concat中使用sum和joins

ios - -[MPMoviePlayerControllerNew _moviePlayerDidBecomeActiveNotification :] 中的断言失败

ios - 单击按钮更改标签上的文本

ios - Swift 更喜欢LargeTitles 在 WKWebview 加载时折叠

iphone - 如何从任何 UIView 的 UITableView 样式分组中获取背景图像?

c# - 使用 LINQ 访问表中的第一个元素时出现 SQLite 错误

带有 XCode 8 GM 的 iOS 10 导致 NSUserDefaults 间歇性无法工作

iphone - 这是用于 iOS WebView 用户代理的 Facebook 吗?

iphone - 如何控制使用 AVMutableComposition 组装的视频的方向