ios sqlite返回数据无法显示

时间:2014-08-13 11:00:36

标签: ios sqlite

我是iOS新手,我使用的是SQLite,我已成功在数据库中插入数据,但当我尝试获取数据和NSLog时,我得到:<Person: 0x10f2242e0>

我该怎么办?如何获取数据?

这是我的代码:

-(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;
}

return persons;之前的最后一行是我打印数据的地方。

1 个答案:

答案 0 :(得分:1)

Persons是代码中的一个对象数组,它包含各种person个对象;因此NSLog,你只会看到对象的内存地址。如果要查看数组中各个person对象的属性,则需要执行以下操作:

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

您可以使用for循环遍历数组中的所有项目并修改其属性。

搜索并阅读相关文档,并在此方面查看其他类似的问题和答案: How to display an array of object with NSLogDumping contents of Array Obj-c to consoler