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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:20:46+00:00 2026-05-26T21:20:46+00:00

I am trying to create a class to abstract my SQLite methods from my

  • 0

I am trying to create a class to abstract my SQLite methods from my business logic etc. However, I can’t seem to ever access my database because it appears as if it is never being created. Please see the code below. I open the database at the specified path. Then I close it later on. After the close, I use the terminal to inspect my Documents directory (within the app). At this point there is no file where I specified it. Does it only create the file when I insert data to the database? Or am I not properly creating the database?

#import "SQLController.h"

@implementation SQLController
@synthesize database;

-(id)initWithDefaultDB {    
    return [self initWithDBPath:[self dbFilePath]];
}

-(id)initWithDBPath:(NSString *)dbPath {
    self = [super init];
    if(self) {
        sqlite3 *db;    
        const char *dbPathCString = [dbPath UTF8String];
        int result = sqlite3_open(dbPath, &db);
        if (result != SQLITE_OK) {
            NSString *errorTag = @"Failed to open database at ";
            NSString *errorMessage = [errorTag stringByAppendingString:dbPath];
            NSLog(errorMessage);
            sqlite3_close(db);
        } else {
            const char *createQuery = "CREATE TABLE IF NOT EXISTS items (title TEXT, details TEXT, category TEXT);";
            self.database = db;
            char *error;
            if (sqlite3_exec(self.database, createQuery, NULL, NULL, &error) != SQLITE_OK) {
                sqlite3_close(self.database);
                NSLog(@"Failed to create table");
                NSAssert(0,@"Error creating table: %s", error);
            }
        }
    }
    return self;
}

-(NSString *)dbFilePath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return [documentsDirectory stringByAppendingPathComponent:@"testdb.sqlite"];
}


@end
  • 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-26T21:20:47+00:00Added an answer on May 26, 2026 at 9:20 pm

    Do you ever write to the DB before closing it? Sqlite is lazy – it doesn’t create the DB until first write.

    Another thing to look at is your creating the C string (dbPathCString) but not passing it into open (you’re passing dbPath).

    -(id)initWithDBPath:(NSString *)dbPath {
    self = [super init];
    if(self) {
        sqlite3 *db;    
        const char *dbPathCString = [dbPath UTF8String];
        int result = sqlite3_open(dbPath, &db);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm trying to create a new class that inherits from an abstract superclass (contains
is that possible create an abstract class from DbQuery and IDbSet? I'm trying to
I am trying to create a base class where I can inherit from it
I am trying to create a class in VB.NET which inherits a base abstract
I'm trying to create a class that can instantiate arrays at runtime by giving
I'm trying to create a point class which defines a property called coordinate. However,
I'm trying to create a test class which organizes its test methods using inner
I'm new to PHP, and was trying to create an Abstract class with a
I'm trying to create an abstract class that defines a property with a getter.
I'm trying to create an abstract class, defining an abstract method, that is to

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.