我需要两次使用sqlite3 finalize语句吗?

时间:2012-02-24 11:01:46

标签: ios sqlite ios4

  //==================================================================
  - ( BOOL ) addNewSimpleTemplates:(NSString*)dbPath:(NSString*)title{
  //==================================================================

  BOOL returnVal = NO;

  NSString *maxValuePosition;

  if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) 
  {
    NSString *selectSQL = [NSString stringWithFormat:@"select MAX(pr.position) FROM phrase_reference pr inner join storyboard_main_categories smc on smc.id = pr.main_category_id where smc.category_name = %@", @"'Simple Templates'"];

    const char *sql = [selectSQL UTF8String];

    sqlite3_stmt *selectStmt;

    if(sqlite3_prepare_v2(database, sql, -1, &selectStmt, NULL) == SQLITE_OK){

      while(sqlite3_step(selectStmt) == SQLITE_ROW) 
      {     
         ///
           // how to use finalise when using sqlite3_prepare_v2

             char *localityChars = (char*)sqlite3_column_text(selectStmt, 0);
         if (localityChars == NULL)
            maxValuePosition = nil;
          else
        maxValuePosition = [NSString stringWithUTF8String: localityChars];

        // increment the postion value
        int postion = [maxValuePosition intValue] + 1;

        //saving a new simple phrase is started here
        sql = "insert into storyboard_phrases(phrase) Values(?)";

        selectStmt = nil;

                 if(sqlite3_prepare_v2(database, sql, -1, &selectStmt, NULL) == 
                 SQLITE_OK){
               sqlite3_bind_text(selectStmt, 1, [title UTF8String], -1, 
                   SQLITE_TRANSIENT);
             }

          if(sqlite3_step(selectStmt) != SQLITE_DONE ) {

              NSLog( @"Error: %s just here itself", sqlite3_errmsg(database) );

          } else {
              NSLog( @"Insert into row id = %d", 
                   sqlite3_last_insert_rowid(database));
               } 
         ///
      }
     }

sqlite3_finalize(selectStmt);

}
  sqlite3_close(database);

}

我想知道如何使用,我的意思是多次使用sqlite3_prepare_v2时多少次输入sqlite 3 finalize语句。

这里在我上面的方法中,我使用sqlie3准备v2语句两次,所以我需要两次使用sqlite3 finalize语句吗?

请告诉我

1 个答案:

答案 0 :(得分:0)

您必须先在sqlite3_finalize上致电selectStmt,然后再将其设为nil