1

I am new to iOS , I am using SQLite and I've succeeded inserting data in database, but when I try to get data and NSLog it, I get: <Person: 0x10f2242e0>

What should I do? How to get data?

Here is my code:

-(NSArray*)getPersons
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:@"SleepCycle&time.db"];

    // Check to see if the database file already exists
    bool databaseAlreadyExists = [[NSFileManager defaultManager] fileExistsAtPath:databasePath];

    // Open the database and store the handle as a data member
    if (sqlite3_open([databasePath UTF8String], &databaseHandle) == SQLITE_OK)
    {
    }




    // Allocate a persons array
    NSMutableArray *persons = [[NSMutableArray alloc]init];

    // Create the query statement to get all persons
    NSString *queryStatement = [NSString stringWithFormat:@"SELECT ID, ADATE, STIME, ETIME ,PER,TOTALTIME,TOTALTIMEMINUTE FROM PERSON"];

    // Prepare the query for execution
    sqlite3_stmt *statement;
    if (sqlite3_prepare_v2(databaseHandle, [queryStatement UTF8String], -1, &statement, NULL) == SQLITE_OK)
    {
        // Iterate over all returned rows
        while (sqlite3_step(statement) == SQLITE_ROW) {

            // Get associated address of the current person row
            int personID = sqlite3_column_int(statement, 0);



            // Create a new person and add it to the array

            Person *person = [[Person alloc]GetAllData:[NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 1)]
                                        andStime:[NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 2)]
                                        andEtime:[NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 3)]
                                         andper:[NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 4)]
                              andtotaltime:[NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 5)]
                              andtotaltimeMinute:[NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 6)]
                                             andIdd:[NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 0)] ];



            // try for id
           /* Person *person = [[Person alloc]Getdata:[NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 1)]
                                                  andLastName:[NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 2)]
                                                  andBirthday:[NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 3)]
                                         andAddress:[NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 4)]
                                             andIdd:[NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 0)] ];
            */
            [persons addObject:person];


        }
        sqlite3_finalize(statement);
    }
    // Return the persons array ----------------------------

    NSLog(@"%@",persons);

    return persons;
}

The last line before return persons; is where I print data.

Neeku
  • 3,646
  • 8
  • 33
  • 43

1 Answers1

1

Persons is an array of object in your code, and it contains various person objects in it; therefore NSLoging it, you'll only see the memory address of the object. If you want to see the properties of individual person objects in your array, you need to do something like:

NSLog(@"Person 1:, %@", [persons objectAtIndex:0].lastName); 
//Based on Objective-C naming conventions you better use "lastName" rather than "LastName", etc.

You can use a for loop to go through all the items in your array and pring their properties.

Search and read the related documentations, and have a look at some other similar questions and answers in this regards: How to display an array of object with NSLog or Dumping contents of Array Obj-c to consoler.

Community
  • 1
  • 1
Neeku
  • 3,646
  • 8
  • 33
  • 43