1

我是 iPhone 应用程序开发人员,现在我们了解了数据库的概念。我想打印数据库中的所有数据。但我不知道如何打印所有数据。这是我害了一些代码。请给我正确的方向来打印所有数据..

例如,在 sql 中,我们将所有数据打印为“select * from contact5;” 我们触发这个字符串。我们可以在 iPhone 编码中完成吗?

-(IBAction)PrintData:(id)sender
{

    NSLog(@"Button Pressed");
    sqlite3_stmt    *statement1;



    NSString *querySQL=@"SELECT * FROM CONTACT5";


    const char *query_stmt = [querySQL UTF8String];


    sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement1, NULL);

    while (sqlite3_step(statement1) == SQLITE_ROW)
    {

        NSLog(@"Enter in  the denger zone");
        NSString *idField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement1, 0)];

        NSString *addressField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement1, 2)];

        NSString *NameField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement1, 1)];

        NSString *phoneFiels = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement1, 3)];

        NSLog(@"ID is=%@",idField);

        NSLog(@"Name is=%@",NameField);
        NSLog(@"Address is=%@",addressField);
        NSLog(@"Phone No. is=%@",phoneFiels);


        [idField release];
        [NameField release];
        [phoneFiels release];
        [addressField release];
    }
    sqlite3_finalize(statement1);
    sqlite3_close(contactDB);
}

提前致谢。

4

1 回答 1

0

我们正在分享一些使用 NSMutableDictionary 来存储您的数据的来,您可以轻松地使用该代码来打印您的数据,您想要在哪里

if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
    {
        const char *sql = "Select * from CalculateTime";

        sqlite3_stmt *insertStmt;

        if(sqlite3_prepare_v2(database, sql, -1, &insertStmt, NULL) != SQLITE_OK)
            NSAssert1(0,@"Error: Failed to prepare statement with message '%s'.",sqlite3_errmsg(database));

        arrayCountry = nil;
        arrayCountry = [[NSMutableArray alloc]init];

        arrayCity = nil;
        arrayCity = [[NSMutableArray alloc]init];


        arrayFlag = nil;
        arrayFlag = [[NSMutableArray alloc]init];

        arrayZone = nil;
        arrayZone = [[NSMutableArray alloc]init];

        NSString *str2;

        while(sqlite3_step(insertStmt)==SQLITE_ROW)
        {

            char *row;

            row = (char*)sqlite3_column_text(insertStmt, 0);
            if(row != NULL)
            {
                str2 = [NSString stringWithUTF8String:row];
                [arrayCountry addObject:str2];
            }

            row = (char*)sqlite3_column_text(insertStmt, 1);
            if(row != NULL)
            {
                str2 = [NSString stringWithUTF8String:row];
                [arrayCity addObject:str2];
            }


            row = (char*)sqlite3_column_text(insertStmt, 2);
            if(row != NULL)
            {
                str2 = [NSString stringWithUTF8String:row];
                [arrayFlag addObject:str2];
            }

            row = (char*)sqlite3_column_text(insertStmt, 3);
            if(row != NULL)
            {
                str2 = [NSString stringWithUTF8String:row];
                [arrayZone addObject:str2];
            }

        }
    }

访问这些字典数据以在任何地方打印..

于 2012-05-28T09:38:58.280 回答