0

我有一个数组,即 _twoArray 它包含如下数据

ha ha 2d array(
    (
    "Anand Kapadiya",
    "Alok Darji",
    "Akash Parikh",
    "Ajay Desai",
    "Aysu Can",
    "Ayegba James",
    "Ashish Modi",
    "Arks Patel",
    "Archit Patel",
    "Anzey Khodorovskyy"
),
    (
    "12/01",
    "08/13/1990",
    "12/09/1989",
    "05/22/1988",
    "04/14/1992",
    "12/15/1905",
    "09/08",
    "05/27/1990",
    "05/22/1990",
    "02/06"
)

)

我有一个包含一个表和 2 个字段的数据库,一个是名称,另一个是出生日期如何从这个二维数组在数据库中添加出生日期和名称我正在尝试以下代码

 for (id obj in _twoArray) 
{

 sqlite3_stmt *stmt;
    int x;

    char *update = "insert into PersonNamesAndBirthDates (Names,Birthdates) values(? ?);";
    x = sqlite3_prepare_v2(database1, update, -1, &stmt, nil);

    if (x == SQLITE_OK)
    {
        NSLog(@"PersonNamesAndBirthDates is -->%@",[NSString stringWithFormat:@"%@",obj]);

        sqlite3_bind_text(stmt, 1, [[NSString stringWithFormat:@"%@",obj] UTF8String],-1, NULL);
        sqlite3_bind_text(stmt, 2, NULL,-1, NULL);

    }
    if (sqlite3_step(stmt) != SQLITE_DONE){}
    NSLog(@"Error: ");
    sqlite3_finalize(stmt);

}   

请给我一些建议,或者如果这不可能,那么告诉我如何同时在表中添加 2 个不同的数组

4

3 回答 3

0

您的 _twoArray 包含两个对象,它们又是 NSArray。现在修复了一个数组中总是有两个数组,然后试试这个

NSArray *nameArr = [_twoArray objectAtIndex:0];
NSArray *bdArr   = [_twoArray objectAtIndex:1];
if (sqlite3_open([databaseFilePath UTF8String], &database1) == SQLITE_OK)
{
    for (int i = 0; i< [nameArr count]; i++)
    {

        sqlite3_stmt *stmt;
        int x;

        char *update = "insert into PersonNamesAndBirthDates (Names,Birthdates) values(? ?);";
        x = sqlite3_prepare_v2(database1, update, -1, &stmt, nil);

        if (x == SQLITE_OK)
        {
            NSLog(@"PersonName is -->%@",[NSString stringWithFormat:@"%@",[nameArr objectAtIndex:i]]);
            NSLog(@"BirthDates is -->%@",[NSString stringWithFormat:@"%@",[bdArr objectAtIndex:i]]);



            sqlite3_bind_text(stmt, 1, [[NSString stringWithFormat:@"%@",[nameArr objectAtIndex:i]] UTF8String],-1, NULL);
            sqlite3_bind_text(stmt, 2, [[NSString stringWithFormat:@"%@",[bdArr objectAtIndex:i]] UTF8String],-1, NULL);

        }
        if (sqlite3_step(stmt) != SQLITE_DONE){}
        NSLog(@"Error: ");
        sqlite3_finalize(stmt);

    }
    sqlite3_close(database1);
}
于 2013-03-11T10:26:26.870 回答
0

您的数组包含两个数组类型的对象。

 for (int i=0;i<[[_twoArray objectAtIndex:0] count];i++) 
{

 sqlite3_stmt *stmt;
    int x;

    char *update = "insert into PersonNamesAndBirthDates (Names,Birthdates) values(? ?);";
    x = sqlite3_prepare_v2(database1, update, -1, &stmt, nil);

    if (x == SQLITE_OK)
    {
        NSLog(@"PersonName:%@ BirthDate:%@",[[NSString stringWithFormat:@"%@",[[_twoArray objectAtIndex:0] objectAtIndex:i]],[[NSString stringWithFormat:@"%@",[[_twoArray objectAtIndex:1] objectAtIndex:i]]);

        sqlite3_bind_text(stmt, 1, [[NSString stringWithFormat:@"%@",[[_twoArray objectAtIndex:0] objectAtIndex:i]] UTF8String],-1, NULL);
        sqlite3_bind_text(stmt, 2, NULL,-1, NULL);

    }
    if (sqlite3_step(stmt) != SQLITE_DONE){}
    NSLog(@"Error: ");
    sqlite3_finalize(stmt);

}   
于 2013-03-11T10:27:08.677 回答
0

您不能以您尝试的方式执行此操作,因为您的for循环一次为您提供一个数组而不是变量。您可以使用以下两种方式:

  1. 创建两个数组namedob分别在同一索引上添加记录然后for对任何数组的索引使用循环并使用2个字符串变量来存储相同索引的数据,然后使用这些来创建查询

  2. 或者,您可以创建一个NSDictionary可以存储的namedob,例如

        NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:0];
        while (records) {
            NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                        @"Mr. X", @"name"
                                        @"13/12/82", @"dob"
                                        nil];
            [array addObject:dictionary];
            records--;
        }
    

    然后为索引创建每个查询。要检索字典、值并创建查询,请使用以下代码,

        for (NSDictionary *dict in array) {
            NSString *userName = [dict valueForKey:@"name"];
            NSString *dateOfBirth = [dict valueForKey:@"dob"];
            //write query here
        }
    
于 2013-03-11T10:44:19.987 回答