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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:12:43+00:00 2026-05-17T02:12:43+00:00

I use the code below to insert data into my sqlite-database but for some

  • 0

I use the code below to insert data into my sqlite-database but for some reason the application always crashes as soon as the method gets called and the problem seems to be the "sqlite3_open" statement. Does anyone have any idea what I might be doing wrong?

- (void)viewDidLoad 
{
    [super viewDidLoad];

    self.title = @"Animals";
    animalsArray = [[NSMutableArray alloc] init];
    animalDetailController = [[AnimalDetailViewController alloc] init];
    addAnimalController = [[AddAnimalViewController alloc] init];
    addAnimalController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    UIBarButtonItem *addItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addAction)];
    self.navigationItem.rightBarButtonItem = addItem;

    databaseName = @"AnimalDatabase.sql";

    NSArray *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentsPath objectAtIndex:0];
    databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
    [self checkAndCreateDatabase];

    [self readDataFromDatabase];

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)addAction{  
    [self presentModalViewController:addAnimalController animated:YES];     
}

- (void)checkAndCreateDatabase{     
    BOOL databaseIsSaved;   
    NSFileManager *fileManager = [NSFileManager defaultManager];
    databaseIsSaved = [fileManager fileExistsAtPath:databasePath];  
    if (databaseIsSaved == YES) {
        return;
    }
    else {
        NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
        [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
    }
    [fileManager release];
}

- (void)readDataFromDatabase{

    sqlite3 *database;
    if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
            const char *sqlStatement = "select * from animals";
            sqlite3_stmt *compiledStatement;
            if (sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {

                    while (sqlite3_step(compiledStatement) == SQLITE_ROW) {
                            [animalsArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)], @"name", [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)], @"description", nil]];
                    }
            }

            sqlite3_finalize(compiledStatement);
    }
    sqlite3_close(database);
}

- (void)writeToDatabase{    
    sqlite3 *database;
    if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
            const char *sqlStatement = "insert into animals(name, description) VALUES(?, ?)";
            sqlite3_stmt *compiledStatement;                
            if (sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK){
                    sqlite3_bind_text(compiledStatement, 1, [addAnimalController.animalName UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_text(compiledStatement, 2, [addAnimalController.animalDescription UTF8String], -1, SQLITE_TRANSIENT);
            }

            if(sqlite3_step(compiledStatement) != SQLITE_DONE ) {
                    NSLog( @"Error: %s", sqlite3_errmsg(database) );
            } else {
                    NSLog( @"Insert into row id = %d", sqlite3_last_insert_rowid(database));
            }
            sqlite3_finalize(compiledStatement);
    }
    sqlite3_close(database);
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (addAnimalController.saveButtonPressed == YES && [addAnimalController.animalName length] != 0 && [addAnimalController.animalDescription length] != 0) {
            [animalsArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:addAnimalController.animalName, @"name", addAnimalController.animalDescription, @"description", nil]];
            [self.tableView reloadData];

            [self writeToDatabase];
            addAnimalController.saveButtonPressed = NO;
        }
}
  • 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-17T02:12:43+00:00Added an answer on May 17, 2026 at 2:12 am

    You need a retain when you set databasePath.

    databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
    

    Should be

    databasePath = [[documentsDir stringByAppendingPathComponent:databaseName] retain];
    

    Make sure you release it in your viewDidUnload and dealloc implementations. (It’s probably smart to release it before you set it as well.)

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

Sidebar

Related Questions

I have below code to insert a style into DOM (there is a use
I can insert a row by using code below. USE pmdb; INSERT INTO md5_tbl
As a PHP developer, I always use the code below whenever I want to
I use below code to update listview. setListAdapter(MyAdapter); MyAdapter.notifyDataSetChanged(); getListView().setEmptyView(empty); But each time update,
I'm trying to insert some data into SQL from Excel VBA. The SQL commands
I'm using code similar to the one below to insert into a mysql DB
I'm using PHP for some simple shell scripting to convert existing data into SQLite
I have two codes to put data into database but it is generating error,
i use the code below to open a new window when a user drops
I use this code below to hide stream url of songs for my wordpress

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.