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

  • Home
  • SEARCH
  • 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 9105785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:11:04+00:00 2026-06-17T02:11:04+00:00

I am using sqlite database mathFActs in my project which i create through Sqlite

  • 0

I am using sqlite database mathFActs in my project which i create through Sqlite Database Browser and add it to Xcode. following is my code from a view controller class

- (void)viewDidLoad
{   [super viewDidLoad];

    [self copyDatabaseIfNeeded];
    [self getInitialDataToDisplay:[self getDBPath]];
}


- (void) copyDatabaseIfNeeded {
    //Using NSFileManager we can perform many file system operations.
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;

    NSString *dbPth = [self getDBPath];
    BOOL success = [fileManager fileExistsAtPath:dbPth];

    if(!success) {

        NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"mathFActs"];
        success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];

        if (!success)
            NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    }
}
- (NSString *) getDBPath {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    return [documentsDir stringByAppendingPathComponent:@"mathFActs"];

}

-(void) getInitialDataToDisplay:(NSString *)databasePath{

    if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
      NSLog(@"open");
    const char *sql = "select Question  from math ";
        sqlite3_stmt *selectstmt;
        if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
            NSLog(@"prepare");
            while(sqlite3_step(selectstmt) == SQLITE_ROW) {

                NSString *addressField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(selectstmt, 0)];
                //address.text = addressField;

                qstn.text=addressField;         
               sqlite3_finalize(selectstmt); 

            }}
        else
            sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
    }

}

when i run the project it print open but not prepare means it’s not executing query statement .. plz help me to solve my problem

  • 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-17T02:11:05+00:00Added an answer on June 17, 2026 at 2:11 am

    Try this :-

    -(void) getInitialDataToDisplay:(NSString *)databasePath
    {
        if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
         {
          NSLog(@"open");
          const char *sql = "select Question  from math ";
          sqlite3_stmt *selectstmt;
          if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
                NSLog(@"prepare");
                while(sqlite3_step(selectstmt) == SQLITE_ROW) {
    
                    NSString *addressField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(selectstmt, 0)];
                    //address.text = addressField;
    
                    qstn.text=addressField;         
    
    
                }
                sqlite3_reset(selectstmt);
           }
           else
           {
              NSLog(@"Error: failed to select details with message '%s'.", sqlite3_errmsg(database));
           }
           sqlite3_finalize(selectstmt);
           sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
        }
    
    }
    

    EDIT :-

    -(void)copyDatabaseIfNeeded
    {
        @try
        {
            NSFileManager *fmgr=[NSFileManager defaultManager];
            NSError *error;
            NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
            NSString *path=[paths objectAtIndex:0];
            dbPath=[path stringByAppendingPathComponent:@"mathFActs.sqlite"];
            if(![fmgr fileExistsAtPath:dbPath]){
                NSString *defaultDBPath=[[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@"Addict.sqlite"];
                if(![fmgr copyItemAtPath:defaultDBPath toPath:dbPath error:&error])
                    NSLog(@"failure message----%@",[error localizedDescription]);
            }
    
        }
        @catch (NSException *exception)
        {
            NSLog(@"Exception: %@", exception);
        }
    
    }
    
    -(NSString *)getDBPath
    {
        //Search for standard documents using NSSearchPathForDirectoriesInDomains
        //First Param = Searching the documents directory
        //Second Param = Searching the Users directory and not the System
        //Expand any tildes and identify home directories.
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
        NSString *documentsDir = [paths objectAtIndex:0];
        return [documentsDir stringByAppendingPathComponent:@"mathFActs.sqlite"];
    }
    
    -(void)openDatabase
    {
        [self copyDatabaseIfNeeded];
    
        if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
        {
            //Database Opened
            NSLog(@"Database opened");
        }
        else
        {
            NSLog(@"Database cannot be opened");
        }
    }
    
    -(void)closeDatabase
    {
        sqlite3_close(database);
    }
    

    Hope it helps you

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

Sidebar

Related Questions

I am using sqlite database with my app following is my code which is
When using SQLite database from Javascript you will use the following statement to get
I am using sqlite database. I used the following code to store three values
I have a SQLite DB designed using SQLite Database Browser 2.0 b1 I have
Hi In my application I am using SQLITE database, I want to add multiple
I have been working with iPhone application in which i am using sqlite database.
I have simple login form I am using SQLite database which has fields like
I am following the article Using SQLite Embedded Database with Entity Framework and Linq-to-SQL
I am using sqlite database in my application in which I am storing a
I am using SQLite database to store few Strings. I am using this code

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.