1

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

6
  • in your else statement write NSLog(@"Error: failed to select details with message '%s'.", sqlite3_errmsg(database)); and tell me error Commented Jan 5, 2013 at 8:26
  • its giving message...'file is encrypted or is not a database Commented Jan 5, 2013 at 8:35
  • what message? print here.. Commented Jan 5, 2013 at 8:36
  • 'file is encrypted or is not a database' Commented Jan 5, 2013 at 8:41
  • wherever you have written mathFActs replace by mathFActs.sqlite Commented Jan 5, 2013 at 8:43

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

still its giving error .... Error: failed to select details with message 'file is encrypted or is not a database'.
use this code I added above and delete your application from your device clean it and rebuild and test it. It will work
so is your issue solved, is soved mark this answer as completed, as this helps others to refer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.