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 8658739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:50:29+00:00 2026-06-12T15:50:29+00:00

I have an app which pretty much follows the method described here . The

  • 0

I have an app which pretty much follows the method described here. The key code is as follows:

#import <Foundation/Foundation.h>
#import <sqlite3.h>     
@interface FailedBankDatabase : NSObject {
    sqlite3 *_database;
}     
+ (FailedBankDatabase*)database;
- (NSArray *)failedBankInfos;    
@end

#import "FailedBankDatabase.h"
#import "FailedBankInfo.h"
@implementation FailedBankDatabase
static FailedBankDatabase *_database;

+ (FailedBankDatabase*)database {
if (_database == nil) {
    _database = [[FailedBankDatabase alloc] init];
}
return _database;
}

- (id)init {
if ((self = [super init])) {
    NSString *sqLiteDb = [[NSBundle mainBundle] pathForResource:@"banklist" 
        ofType:@"sqlite3"];

    if (sqlite3_open([sqLiteDb UTF8String], &_database) != SQLITE_OK) {
        NSLog(@"Failed to open database!");
    }
}
return self;
}

- (void)dealloc {
    sqlite3_close(_database);
    [super dealloc];
}

Now, the app works with one database as expected. But, I want to be able to switch to a different database when the user touches a button. I have the button handler and logic OK, and I store the name of the database to be used and can retrieve it. But, no matter what I do, I always get the same (original) database being called. I fear that the handle associated with _database, a object of type sqlite3, in the example is not being changed properly, so I don’t open the database properly. How should I go about changing this? You can’t re-init a singleton, but I need to change what’s stored in it, in this case _database. Thanks.

EDIT: I would add that if I ask for _database is a pointer. So I need to open a new database (and close the first I guess) and give the new database a new address in the process.

  • 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-06-12T15:50:30+00:00Added an answer on June 12, 2026 at 3:50 pm

    I had the same problem, but couldn’t modify the database (they were used in other projects).

    So, I created a method called useDatabase:, that close the previous connection, and open a new one.

    The steps :
    Your – (id)init remains the same

    In FailedBankDatabase, you create a method that close and open the database with the name of the new database

    -(void)useDatabase:(NSString*)database {
    
        sqlite3_close(_database);
    
        NSString *sqLiteDb = [[NSBundle mainBundle] pathForResource:database 
        ofType:@"sqlite3"];
    
        if (sqlite3_open([sqLiteDb UTF8String], &_database) != SQLITE_OK) {
            NSLog(@"Failed to open database!");
        }
    
    }
    

    At the very beggining (for example in appDidFinishLaunching), you call the singleton once

    [FailedBankDatabase database];

    , so that it is first initialised.

    Then, when you want to change the .sqlite used, you can call :

    [FailedBankDatabase useDatabase:@”anOtherDatabase”]

    I think you can do this when you don’t have to change the database very often. In my case, I use this once at the very first screen, with 3 buttons, where I will choose wich database will be used.

    For more complicated cases, for exemple involving multithreading, you should not do that since it closes the connection for a little time, while it is used elsewhere.

    Hope it helps,

    Jery

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

Sidebar

Related Questions

I have a very simple Core Data app which pretty much contains strings of
I have an iPhone app which pretty much is a mobile app for a
I have a question that has pretty much been asked here: asp.net mvc Html.ActionLink()
I have an app that uses UIViewControllers for everything pretty much. So far I
I have an app that's pretty much a large presentation of a companies product.
I have app in which i have recorded sound files i want that i
I have a app which will download a file from web. The download action
I have an app which has a map and some POI on it. I'm
I have an app which contain some images in res/drawable/ and shows them in
I have an app which access an app folder on a per user basis.

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.