从数据库中获取数据。

时间:2011-10-01 05:57:48

标签: iphone mysql database ios uitableview

好的,我可以从我的iPhone应用程序连接到mysql数据库,我已经在tableview中进行了设置。因此,当我调用didSelectRow方法时,它会存储用户ID号,而不是indexRow号。我的问题是如何使用存储的id来检索其余信息,因此当按下该行时,它会向其他用户显示特定信息。我知道我在这里没有显示任何代码,因为我是从iPad上写的,所以我希望你能跟随我想要做的事情和帮助。谢谢。

2 个答案:

答案 0 :(得分:1)

我可以用一种方式帮助你,你必须将来自数据库的数组存储在appdel中的数组中,你可以随时调用该数组,我们希望示例代码在这里

这是appdb中的数组

NSMutableArray *fruit_details;

在Appdb中调用一个方法

[self readStudentFromDatabase];

然后在该方法中编写代码

NSArray *documentpaths= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentDir = [documentpaths objectAtIndex:0];

databasePath = [documentDir stringByAppendingPathComponent:databaseName];
fruit_details=[[NSMutableArray alloc]init];
//open the database from the users filesystem
if(sqlite3_open([databasePath UTF8String], &database)==SQLITE_OK)
{
    // Setup the SQL Statement and compile it for faster access
    const char *sqlStatement ="select * from stu";
    sqlite3_stmt *compiledStatement;
    if(sqlite3_prepare_v2(database, sqlStatement,-1, &compiledStatement , NULL)== SQLITE_OK)
    {
        // Loop through the results and add them to the feeds array
        while (sqlite3_step(compiledStatement)==SQLITE_ROW) 
        {
            NSString *aName =[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,0)];
            NSString *amarks=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,1)];
            NSString *arank =[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,2)];
            NSString *aaddr =[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,3)];
            NSString *aemail =[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,4)];
            NSString *aphno =[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,5)];
            NSString *aage =[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,6)];
            NSString *asex =[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,7)];
            NSString *adate =[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,8)];
            NSString *aimage=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 9)];
            // // Create a new animal object with the data from the database

        //animal *animal_object=[[animal alloc]initWithName:aName description:arollNO url:amarks];

            //add the animal object to the animal ar
            //[animals addObject:animal_object];
            temp=[[NSMutableArray alloc]init];
            [temp addObject:aName];
            [temp addObject:amarks];
            [temp addObject:arank];
            [temp addObject:aaddr];
            [temp addObject:aemail];
            [temp addObject:aphno];
            [temp addObject:aage];
            [temp addObject:asex];
            [temp addObject:adate];
            [temp addObject:aimage];
            [fruit_details addObject:temp];
        }
    }
    sqlite3_finalize(compiledStatement);

}
sqlite3_close(database);

我认为这可能对你有帮助.....

答案 1 :(得分:0)

存储ID 你从db获得了什么?我的理解是,当用户选择一行时,您希望转到新视图并使用存储的ID 从数据库中获取相应的信息。