0

I am using SQLite database (firefox addon) to my iOS development. I insert data manually to my database. but first time run the program , show that data. but after insert again data to database in manually that data is not showing. I'm beginner for ios development. Bellow has my database connection. please help.

- (void)viewDidLoad
{
    NSString *defaultDBPath;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSString *ddPath = [self getDBPath];
    BOOL success = [fileManager fileExistsAtPath:ddPath];

    if(!success) {

        defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"AddressBook.sqlite"];
        success = [fileManager copyItemAtPath:defaultDBPath toPath:ddPath error:&error];

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

   // const char *dbpath=[ddPath UTF8String];
    const char *dbpath1=[ddPath UTF8String];
    sqlite3_stmt *statement;
     NSMutableArray  *company =[[NSMutableArray alloc]init];

    if (sqlite3_open(dbpath1, &contactDB)==SQLITE_OK)
    {
        NSString *querySQL=[NSString stringWithFormat:@"SELECT CompanyName FROM tblCompany order by CompanyName"];

        const char *query_stmt=[querySQL UTF8String];

        if (sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL)==SQLITE_OK)
        {
            int i=0;
            while(sqlite3_step(statement)==SQLITE_ROW)
            {
                NSString *my=[[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 0)];
                NSLog(@"name %@",my);


               [company insertObject:my atIndex:i];
               i=i+1;
                companyDetails=[NSDictionary dictionaryWithContentsOfFile:my];

               // [[cell textlabel] setText:my];
            }
            companies =company;
            companyKey=[companyDetails allKeys];
            sqlite3_finalize(statement);
        }
        else
            NSLog(@"Not connected");
        sqlite3_close(contactDB);
    }

Getdatabase path method ::

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

I tried these but ti is not showing updated database data in my iOS application. Please give me a solution.

5
  • Make sure that you are making changes in ddPath Commented Jan 8, 2013 at 3:45
  • You are only copying the database once. After that it exists in your doc directory and will not be copied again. Commented Jan 8, 2013 at 3:54
  • This is because each time you are running your application a new sqlite file is created. Commented Jan 8, 2013 at 4:29
  • If you are adding manually to the .sqlite file in your project then you need to reset the simulator. Else if you are opening the file from finder and then open .sqlite and making changes it wont reflect in your database Commented Jan 8, 2013 at 4:47
  • @Dhara really got the idea from this. thanks ! Commented Jan 8, 2013 at 5:59

1 Answer 1

0

you not check .sqlite file in project folder,to check file path like this

/Users/username/Library/Application Support/iPhone Simulator/5.0/Applications/4F4574E2-AD8A-493E-9B16-2D456AFAEB6C/Documents/your file.sqlite 

//username - your user name
//4F4574E2-AD8A-493E-9B16-2D456AFAEB6C  -it changed for projects
Sign up to request clarification or add additional context in comments.

1 Comment

I found the reason for this. This path was the problem. Thank You !

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.