English 中文(简体)
EXC_BAD_ACCESS DATABASE
原标题:EXC_BAD_ACCESS SQL DATABASE

I got this exc_bad_access problem in AppDelegate.m What I did and want is to have multiple sql query. To avoid complexity, I did two blocks (it will have more) and both are querying different table from sql.

-(void)readDataFromDatabase {

    // Setup the database object
    sqlite3 *database;

    // Initialize the budgetobjects Array
    Part1Array = [[NSMutableArray alloc] init];

    // Open the database from the users filessytem
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {

        // Setup the SQL Statement and compile it for faster access
        const char *sqlStatement = "select * from part1TBL";
        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
                // You can add more rows based on your object
                NSString *Part1_Name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
                NSString *Part1_Description = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
                // Create a new Restaurant with the data from the database
                Part1 *newPart1 = [[Part1 alloc] initWithName:Part1_Name description:Part1_Description];

                // Add the budgetobject to BudgetObjectsrantArray
                [Part1Array addObject:newPart1];

            }
        }

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

    }
    sqlite3_close(database);


    // Setup the database object
    sqlite3 *database2;

    // Initialize the budgetobjects Array
    Part2Array = [[NSMutableArray alloc] init];

    // Open the database from the users filessytem
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {

        // Setup the SQL Statement and compile it for faster access
        const char *sqlStatement = "select * from part2TBL";
        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
                // You can add more rows based on your object
                NSString *Part2_Name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
                NSString *Part2_Description = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
                // Create a new Restaurant with the data from the database
                Part2 *newPart2 = [[Part2 alloc] initWithName:Part2_Name description:Part2_Description];

                // Add the budgetobject to BudgetObjectsrantArray
                [Part2Array addObject:newPart2];

            }
        }

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

    }
    sqlite3_close(database2);





}



@end

The exc_bad_access highlighted at the end where

sqlite3_close(database2);
最佳回答

I m not familiar with sqlite, but it looks like the issue with your code is that you re never opening database2 . Instead, you open database twice, which looks like a typo.

// Open the database from the users filessytem if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {

.......

sqlite3_close(database);

......

// Open the database from the users filessytem if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) { // <--- This should be &database2 ......

sqlite3_close(database2);

EDIT:请注意 同时,在你打算使用数据库2时,你还有几个地点重新使用数据库,而且你可能想考虑仅仅重新使用数据库,或者在适用的情况下将这一功能引向共同功能。

问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签