Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6675211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:49:33+00:00 2026-05-26T03:49:33+00:00

I have an application where I access an SQLite database several times.. But, once

  • 0

I have an application where I access an SQLite database several times.. But, once I’ve accessed the database one time, all following attempts cause the app to crash…
I’m not sure if it’s because the database has not been properly released…

An example, I run a search to populate a tableview with names of artists. Once I select an artist, I’m navigated to a new tableview, where I want to populate it with the artist’s works.

But here’s the problem. I access the database to populate the first view, but when I want to populate the second view, it doesn’t enter sqlite3_prepare_v2 of the query… so this must mean the database is still in use by the old query..

So what is the proper way of handling closing a database after use?

Currently I do a query like this:

-(NSArray *)findAllArtists
{
    NSMutableArray *returnArray = [[[NSMutableArray alloc] init] autorelease];

    NSString *query = @"SELECT * FROM Painting GROUP BY Artist";

    sqlite3_stmt *statement;

    if (sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, nil) 
    == SQLITE_OK) 
    {        
        while (sqlite3_step(statement) == SQLITE_ROW) 
        {
            char *uniqueIdChars = (char *) sqlite3_column_text(statement, 0);
            char *artistChars = (char *) sqlite3_column_text(statement, 1);
            NSString *uniqueId = [[NSString alloc] initWithUTF8String:uniqueIdChars];
            NSString *artist = [[NSString alloc] initWithUTF8String:artistChars];

            PaintingInfo *info = [[PaintingInfo alloc] initWithUniqueId:uniqueId artist:artist];
            [returnArray addObject:info];

            [uniqueId release];
            [artist release];
        }

        sqlite3_finalize(statement);
    }

    sqlite3_close(database);
    return returnArray;    
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:49:34+00:00Added an answer on May 26, 2026 at 3:49 am

    You should look at fmdb wrapper at github. Even if you don’t use it, look at the code.

    Where are you opening the database? You’re closing it in this code. Before you call it again, it needs to be open. You should consider just keeping it open for the duration of the single user iOS app and closing when you’re done. What happens if you simply remove the close call?

    The first thing you should do is check all your return codes for sqlite calls. for example, with step you’re not handling anything other than SQLITE_ROW. At least log others. Also for finalize and close you’re not handling or logging others.

    Also, you’re preparing (compiling) the sql statement but your not saving it off. prepare_v2 gives you back a compiled statement. Save it off as a member variable and call reset against it before using it again.

    To answer your specific question of how to close – you need to consider that some statements may not have been finalized. Here’s my close method: (BTW, ENDebug is my wrapper over NSLog)

    - (void)close
    {
        if (_sqlite3)
        {
            ENInfo(@"closing");
            [self clearStatementCache];
    
            int rc = sqlite3_close(_sqlite3);
            ENDebug(@"close rc=%d", rc);
    
            if (rc == SQLITE_BUSY) 
            { 
                ENError(@"SQLITE_BUSY: not all statements cleanly finalized");
    
                sqlite3_stmt *stmt; 
                while ((stmt = sqlite3_next_stmt(_sqlite3, 0x00)) != 0) 
                {
                    ENDebug(@"finalizing stmt");
                    sqlite3_finalize(stmt); 
                }
    
                rc = sqlite3_close(_sqlite3);
            }
    
            if (rc != SQLITE_OK)
            {
                ENError(@"close not OK.  rc=%d", rc);
            }
    
            _sqlite3 = NULL;
        }
    }
    

    finally, consider adding much more logging along with the return codes so you can get more insight.

    Hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to have an application access a SQL Compact database file
We have a job management application running (Access) with the database in SQL Server
I have an application I’m writing in access with a SQL server backend. One
I currently have an MS Access application that connects to a PostgreSQL database via
I have trouble with finding my SQLite Database. As we can access the SQLite
My android application needs frequent access to sqlite database. I was thinking if there
My application is using Qt 4.6 to access sqlite. Now I have a requirement
I have a SQLite Database that I access using System.Data.SQLite . The database is
I have 4 tables in my SQLite database. One with questions, one with answers
I have one Microsoft Access .MDB file and want to use that database in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.