when to call SQLite cleanup functions?

前端 未结 1 567
花落未央
花落未央 2021-01-23 09:30

I am working with sqlite database in iOS.I have used CURD operation in my app.For example to insert data into the database i have used below code.

   - (BOOL) sa         


        
相关标签:
1条回答
  • 2021-01-23 09:59

    There are simple guidelines, and to be blunt, your code doesn't follow any of them at all.

    For each successful call to sqlite3_open you must call sqlite3_close.

    For each successful call to sqlite3_prepare_v2 you must call sqlite3_finalize. Optionally, in between you may call sqlite3_reset zero or more times in cases where you reuse the prepared statement.

    You should always check the result of calls to sqlite3_open and sqlite3_prepare_v2. If they fail, you should use sqlite3_errmsg to log what the problem was.

    Do not build query strings using stringWithFormat. Instead, properly bind values to the query using appropriate calls to the various sqlite3_bind_xxx functions.

    The code you posted violates all of these. You have plenty of cases where you don't close the database or finalize the prepared statement. And you are incorrectly building your query with stringWithFormat:.

    Here's your code all fixed up. Note that I'm assuming all the values you are inserting are strings. Adjust accordingly for any non-string values.

    - (BOOL) saveData:(User *)user
    {
        BOOL result = NO;
    
        if([self getUserData:user.email] != nil)
        {
            [self updateUserData:user];
            result = YES;
        }
        else
        {
            const char *dbpath = [databasePath UTF8String];
    
            if (sqlite3_open(dbpath, &database) == SQLITE_OK)
            {
                const char *insert_stmt = "insert into users(userid,name,email,password,address,age,gender,phone,qualification,role,createddate,apiKey,priorityid,usertype) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    
                if (sqlite3_prepare_v2(database, insert_stmt,-1, &statement, NULL) == SQLITE_OK) {
                    sqlite3_bind_text(stmt, 1, [user.userid UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_text(stmt, 2, [user.name UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_text(stmt, 3, [user.email UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_text(stmt, 4, [user.password UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_text(stmt, 5, [user.address UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_text(stmt, 6, [user.age UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_text(stmt, 7, [user.gender UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_text(stmt, 8, [user.phone UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_text(stmt, 9, [user.qualification UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_text(stmt, 10, [user.role UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_text(stmt, 11, [user.createddate UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_text(stmt, 12, [user.api_key UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_text(stmt, 13, [user.priority_id UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_text(stmt, 14, [user.user_type UTF8String], -1, SQLITE_TRANSIENT);
    
                    if (sqlite3_step(statement) == SQLITE_DONE)
                    {
                        result = YES;
                    }
    
                    sqlite3_finalize(statement);
                }
                else
                {
                    NSLog(@"Unable to prepare statement: %s",sqlite3_errmsg(database));
                }
    
                sqlite3_close(database);
            }
            else
            {
                NSLog(@"Unable to open database: %s",sqlite3_errmsg(database));
            }
        }
    
        return result;
    }
    

    Update based on the new edit:

    Just like your original code, you violate a few of the rules. Your updated code doesn't quite follow the pattern I gave in my answer above.

    The main issue now is that you open the database but you only close it under one of two possible code paths. If inspMap != nil you never close the database.

    You really should refactor the code so you only open the database if inspMap is nil. I did something similar to your original code in my original answer.

    0 讨论(0)
提交回复
热议问题