首页 \ 问答 \ 不执行任何查询SQLite(Not executing any queries SQLite)

不执行任何查询SQLite(Not executing any queries SQLite)

所以我创建了一个连接到SQLite数据库的NSObject,一切都运行良好。 在我的代码中,如果查询是可执行的,则代码会经历一系列条件并执行查询。 然后,无缘无故,我没有做任何事情,可执行查询停止工作。 我尝试删除db文件并再次将其复制(我确保检查所有复选框),但所有发生的事情是project.pbxproj突然出现在我的所有文件中,Xcode仍然无法执行我的可执行查询。 我把project.pbxproj放回到项目文件中,并在没有运气的情况下从项目/中继续删除/重新复制db文件。 这是我的代码,我知道它完全没问题,因为我没有改变任何内容并且它正在工作:

-(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){
                    NSLog(@"busy");
                }else if(executeQueryResults == SQLITE_ERROR){
                    NSLog(@"error in exec");
                }else if(executeQueryResults == SQLITE_MISUSE){
                    NSLog(@"misuse");
                }else if(executeQueryResults == SQLITE_ROW){
                    NSLog(@"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.
        sqlite3_finalize(compiledStatement);

    }

    // Close the database.
    sqlite3_close(sqlite3Database);
}

我创建了所有条件来检查是什么类型的问题,结果是executeQueryResults等于SQLITE_ERROR,错误是:unkown error。 此外,我尝试记录数组,它们是空的或等于null,即使数据库不是空的开始。 此外,我在比较executeQueryResults与SQLITE_DONE / BUSY / ERROR等所有行的错误(仅在此项目中)说:语义问题:常量101与类型'BOOL'(又名'bool')表达式的比较总是为假。 在具有完全相同代码的其他项目中,不会出现此错误。

警告的屏幕截图


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 executable queries stopped working. I tried deleting the db file and copying it back again (I made sure to check all the checkboxes), but all that happened was that the project.pbxproj suddenly appeared in all my files and Xcode still couldn't execute my executable queries. I put the project.pbxproj back in the project file and kept deleting/recopying the db file from/in the project without luck. Here is my code, I know it is perfectly fine because I changed nothing in it and it was working:

-(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){
                    NSLog(@"busy");
                }else if(executeQueryResults == SQLITE_ERROR){
                    NSLog(@"error in exec");
                }else if(executeQueryResults == SQLITE_MISUSE){
                    NSLog(@"misuse");
                }else if(executeQueryResults == SQLITE_ROW){
                    NSLog(@"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.
        sqlite3_finalize(compiledStatement);

    }

    // Close the database.
    sqlite3_close(sqlite3Database);
}

I created all the conditions to check what kind of problem there was, turns out executeQueryResults 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. Also, I am getting an error (ONLY IN THIS PROJECT) at all lines comparing executeQueryResults to SQLITE_DONE/BUSY/ERROR etc saying : semantic issue: Comparison of constant 101 with expression of type 'BOOL' (aka 'bool') is always false. In other projects with exactly the same code, this error does not appear.

Screenshot for warning


原文:https://stackoverflow.com/questions/25477018
更新时间:2023-10-10 14:10

最满意答案

PHP将使用您在HTML中指定的[0]键接收$_POST['use_name']作为数组。 所以你要测试:

if ($_POST['use_name'][0] == 'Test') {
 // ....
}

使用print_r($_POST)进行调试以查看数组的结构。

请注意,如果您不需要在HTML中发送特定的数组键,则只需将输入name='use_name[]' ,PHP就会将它们作为数字索引数组接收。 如果需要,您可以使用字符串键更具体: name='use_name[first_value]' ,PHP将接收字符串键,如$_POST['use_name']['first_value']

但请注意,这不是使用单选按钮的典型方法。 由于通常只选择一个,除非您列出名称为name='use_name[0]的多个单选按钮(我们没有看到您的HTML),否则这些按钮不会被绑定为无线电组。 它们必须具有相同的名称,不同的数组键将它们转换为不同的无线电组。 这更常用于复选框。


PHP will receive $_POST['use_name'] as an array, with the [0] key you specified in the HTML. So you want to test:

if ($_POST['use_name'][0] == 'Test') {
 // ....
}

Debug with print_r($_POST) to see the structure of the array.

Note that if you don't have a need to send specific array keys in the HTML, you can just name the inputs like name='use_name[]' and PHP will receive them as a numeric-indexed array. You can be more specific if needed, with string keys too: name='use_name[first_value]' and PHP will receive the string keys as in $_POST['use_name']['first_value']

Note however, that this is not a typical way to use radio buttons. Since only one is usually selected, unless you are listing multiple radio buttons (we don't see your HTML) all with the name name='use_name[0] the buttons will not be bound as a radio group. They must all have the same name, and different array keys will turn them into different radio groups. This is more commonly used for checkboxes.

相关问答

更多
  • 你需要比较如下: {% if radio.choice_value == 'value1' %} radio.tag是一个返回输入的整个HTML的方法,这就是你的比较失败的原因。 You need to compare as follows: {% if radio.choice_value == 'value1' %} radio.tag is a method that returns the entire HTML of the input, which is why your compariso ...