So I created an NSObject that connects to an SQLite Database and everything was working perfectly. In my code, if the query is executable, the code goes through a series of conditions and executes the query. Then, for no reason and I hadn't done anything, the project.pbxproj suddenly appears in all my files and Xcode can no longer execute my executable queries. Here is my code:

    -(void)runQuery:(const char *)query isQueryExecutable:(BOOL)queryExecutable{
// Create a sqlite object.
sqlite3 *sqlite3Database;

// Set the database file path.
NSString *databasePath = [self.documentsDirectory stringByAppendingPathComponent:self.databaseFilename];

// Initialize the results array.
if (self.arrResults != nil) {
    [self.arrResults removeAllObjects];
    self.arrResults = nil;
self.arrResults = [[NSMutableArray alloc] init];

// Initialize the column names array.
if (self.arrColumnNames != nil) {
    [self.arrColumnNames removeAllObjects];
    self.arrColumnNames = nil;
self.arrColumnNames = [[NSMutableArray alloc] init];

// Open the database.
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 all data from database to memory.
    BOOL prepareStatementResult = sqlite3_prepare_v2(sqlite3Database, query, -1, &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];
        else {
            // This is the case of an executable query (insert, update, ...).

            // Execute the query.
            BOOL executeQueryResults = sqlite3_step(compiledStatement);
            if (executeQueryResults == SQLITE_DONE) {
                // Keep the affected rows.
                self.affectedRows = sqlite3_changes(sqlite3Database);

                // Keep the last inserted row ID.
                self.lastInsertedRowID = sqlite3_last_insert_rowid(sqlite3Database);
            }else if(executeQueryResults == SQLITE_BUSY){
            }else if(executeQueryResults == SQLITE_ERROR){
                NSLog(@"error in exec");
            }else if(executeQueryResults == SQLITE_MISUSE){
            }else if(executeQueryResults == SQLITE_ROW){
            else {
                // If could not execute the query show the error message on the debugger.
                NSLog(@"DB Error: %s", sqlite3_errmsg(sqlite3Database));
    else {
        // In the database cannot be opened then show the error message on the debugger.
        NSLog(@"%s", sqlite3_errmsg(sqlite3Database));

    // Release the compiled statement from memory.


// Close the database.


I created all the conditions to check what kind of problem there was, turns out executableQuery is equal to SQLITE_ERROR, and the error is: unkown error. Also, I tried logging the arrays and they are either empty or equal to null even when the database is not empty to start with. Thanks a lot for the help.

Related posts

Recent Viewed