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

The Archive Base Latest Questions

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

I am building an app which contains a form in one view,in which the

  • 0

I am building an app which contains a form in one view,in which the user fills all the fields and when he clicks the save button the data must be saved in to database and after navigating back,there’s another view which, when entered, must show the saved data(event).

I have created a database and have gone through several sqlite3 tutorials;

I have done all other changes to my code according to my requirement. However, when I use this statement to check whether data is inserted in database:

SELECT * FROM reminders;

I am getting nothing and I am confused whether data is inserted or not.

How do I save it properly, and how do I retrieve data from database to use and display it in other view?

  • 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:35:53+00:00Added an answer on May 26, 2026 at 9:35 pm

    First you should create the sqlite3 database file (check this link), then you should include it into your project. Now to connect to it you can use the following code:

    #pragma mark -
    #pragma mark Create/Load Database
    + (void)createEditableCopyOfDatabaseIfNeeded {
        // First, test for existence.
        NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString * documentsDirectory = [paths objectAtIndex:0];
        NSString * writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"DATABASENAME.DB"];
    
        BOOL success;
        NSFileManager * fileManager = [NSFileManager defaultManager];
        success = [fileManager fileExistsAtPath:writableDBPath];
        if (success) {
            return;
        }
    
        // The writable database does not exist, so copy the default to the appropriate location.
        NSError * error;
        NSString * defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"DATABASENAME.DB"];
        success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
        if (!success) {
            NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
        }
    }
    + (sqlite3 *)getDBConnection {
        [DatabaseController createEditableCopyOfDatabaseIfNeeded];
    
        NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString * documentsDirectory = [paths objectAtIndex:0];
        NSString * path = [documentsDirectory stringByAppendingPathComponent:@"DATABASENAME.DB"];
    
        // Open the database. The database was prepared outside the application.
        sqlite3 * newDBConnection;
        if (sqlite3_open([path UTF8String], &newDBConnection) == SQLITE_OK) {
            //NSLog(@"Database Successfully Opened :)");
        } else {
            //NSLog(@"Error in opening database :(");
        }
        return newDBConnection;
    }
    

    then to insert a record you can use this code:

    + (void)insertEvent:(Event *)newEvent {
        sqlite3 * connection = [DatabaseController getDBConnection];
        const char * text = "INSERT INTO Event (Serial, Name, Date) VALUES (?, ?, ?)";
        sqlite3_stmt * insert_statement;
        int prepare_result = sqlite3_prepare_v2(connection, text, -1, &insert_statement, NULL);
        if ((prepare_result != SQLITE_DONE) && (prepare_result != SQLITE_OK)) {
            // Error
            sqlite3_close(connection);
            return;
        }
    
        sqlite3_bind_int(insert_statement, 1, newEvent.Serial);
        sqlite3_bind_text(insert_statement, 2, [newEvent.Name UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_double(insert_statement, 3, [newEvent.Date timeIntervalSince1970]);
    
        int statement_result = sqlite3_step(insert_statement);
        if ((statement_result != SQLITE_DONE) && (statement_result != SQLITE_OK)) {
            //Error
            sqlite3_close(connection);
            return;
        }
    
        sqlite3_finalize(insert_statement);
    
        // Get the Id of the inserted event
        int rowId = sqlite3_last_insert_rowid(connection);
        newEvent.Id = rowId;
    
        sqlite3_close(connection);
    }
    

    now to get an event:

    + (Event *)getEventById:(int)id {
        Event * result = nil;
        sqlite3 * connection = [DatabaseController getDBConnection];
    
        const char * text = "SELECT * FROM Event WHERE Id = ?";
        sqlite3_stmt * select_statement;
    
        int prepare_result = sqlite3_prepare_v2(connection, text, -1, &select_statement, NULL);
        if ((prepare_result != SQLITE_DONE) && (prepare_result != SQLITE_OK)) {
            // error
            sqlite3_close(connection);
            return result;
        }
    
        sqlite3_bind_int(select_statement, 1, id);
    
        if (sqlite3_step(select_statement) == SQLITE_ROW) {
            result = [[[Event alloc] init] autorelease];
    
            result.Id = sqlite3_column_int(select_statement, 0);
            result.Serial = sqlite3_column_int(select_statement, 1);
            result.Name = (((char *) sqlite3_column_text(select_statement, 2)) == NULL)? nil:[NSString stringWithUTF8String:((char *) sqlite3_column_text(select_statement, 2))];
            result.Date = [NSDate dateWithTimeIntervalSince1970:sqlite3_column_double(select_statement, 3)];
        }
        sqlite3_finalize(select_statement);
    
        sqlite3_close(connection);
        return (result);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a Rails app which creates a bookmarklet file for each user upon
I'm building an AJAX app which animates in content dynamically. Since all links are
I am building an app with complex data which I use arrays for. Some
I am building an app using Twisted in which a server contains some timers
I am building a web app which contains a Google Maps link that can
I am building a Prism app with several modules, one of which ( MyModule
I am building a Facebook App which is heavy on Javascript. For this I
I am building an app for which I need to set up cron jobs.
I am building an iphone app which allows people to update an xml file
I am building an iPhone app in which users (or employees) will be able

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.