【问题标题】:How to delete all the contents from SQLite table in an iOS app如何从 iOS 应用程序中的 SQLite 表中删除所有内容
【发布时间】:2012-10-02 08:00:50
【问题描述】:

我想从我的 iPhone 应用程序的表中删除所有行。我正在使用以下代码,但它没有删除数据:

+(void)emptyData:(NSString*)dbPath{

    NSString *query = @"delete from survey_question_responses";
    const char *sqlStatement = [query UTF8String];
    sqlite3_stmt *compiledStatement;

    if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {

   // Loop through the results and add them to the feeds array
     while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
      // Read the data from the result row
        NSLog(@"result is here");
      }

       // Release the compiled statement from memory
      sqlite3_finalize(compiledStatement);
     }
     }

【问题讨论】:

  • NSString *query1 = [NSString stringWithFormat:@"DELETE from TableName"]; [[DBManager sharedDatabase]executeQuery:query4];

标签: iphone ios sqlite


【解决方案1】:
NSString *query1 = [NSString stringWithFormat:@"DELETE from TableName"];
[[DBManager sharedDatabase]executeQuery:query1];

在哪里

-(void)executeQuery:(NSString*)_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);
}

【讨论】:

    【解决方案2】:

    获取数据库的路径:

    NSString*paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    
    NSString*documentDirectory=[paths objectAtIndex:0];
    
    NSSting*dBPath=[documentDirectory stringByAppendingPathComponent:@"dB.sql"];
    

    打开数据库:

    if(sqlite3_open([dBPath UTF8String], &database) == SQLITE_OK){
    
        NSString*query=[NSString stringWithFormat:@"DELETE from tablename"];
    }
    

    执行查询语句:

    if(sqlite3_exec(database, [query UTF8String],NULL, NULL, NULL) == SQLITE_OK){            
       NSLog(@"Delete Query Success);
    
    }
    else{
    
         NSLog(@"Delete Query Failed);}
    
          sqlite3_close(database);
    
    }
    

    希望这会有所帮助..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-10
      • 2012-04-26
      • 1970-01-01
      • 1970-01-01
      • 2015-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多