dimanche 3 janvier 2016

sqlite3_stmt is null iOS database compiledStatement is null

sqlite3_stmt is null iOS database compiledStatement is null BOOL openDatabaseResult = sqlite3_open([databasePath UTF8String], &sqlite3Database); if(openDatabaseResult == SQLITE_OK) { // Declare a sqlite3_stmt object in which will be stored the query after having been compiled into a SQLite statement. sqlite3_stmt *compiledStatement; load the data code ` BOOL prepareStatementResult = sqlite3_prepare_v2(sqlite3Database, query, 0, &compiledStatement, NULL); if(prepareStatementResult == SQLITE_OK) { // Check if the query is non-executable. if (!queryExecutable){ // In this case data must be loaded from the database.

            // Declare an array to keep the data for each fetched row.
            NSMutableArray *arrDataRow;

            // Loop through the results and add them to the results array row by row.
            while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
                // Initialize the mutable array that will contain the data of a fetched row.
                arrDataRow = [[NSMutableArray alloc] init];

                // Get the total number of columns.
                int totalColumns = sqlite3_column_count(compiledStatement);

                // Go through all columns and fetch each column data.
                for (int i=0; i<totalColumns; i++){
                    // Convert the column data to text (characters).
                    char *dbDataAsChars = (char *)sqlite3_column_text(compiledStatement, i);

                    // If there are contents in the currenct column (field) then add them to the current row array.
                    if (dbDataAsChars != NULL) {
                        // Convert the characters to string.
                        [arrDataRow addObject:[NSString  stringWithUTF8String:dbDataAsChars]];
                    }

                    // Keep the current column name.
                    if (self.arrColumnNames.count != totalColumns) {
                        dbDataAsChars = (char *)sqlite3_column_name(compiledStatement, i);
                        [self.arrColumnNames addObject:[NSString stringWithUTF8String:dbDataAsChars]];
                    }
                }



                // Store each fetched data row in the results array, but first check if there is actually data.
                if (arrDataRow.count > 0) {
                    [self.arrResults addObject:arrDataRow];
                }
            }

Aucun commentaire:

Enregistrer un commentaire