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"
)

)

私は1つのテーブルと2つのフィールドを持つデータベースを持っています.1つは名前で、もう1つは生年月日です.この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);

}   

plz私に何かを提案するか、これが不可能な場合は、テーブルに2つの異なる配列を同時に追加する方法を教えてください

4

3 に答える 3

0

_twoArray には、NSArray である 2 つのオブジェクトが含まれています。これで、1 つの配列内に常に 2 つの配列が存在することが修正されました。次に、これを試してください。

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

配列には、配列型の 2 つのオブジェクトが含まれています。

 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ループは一度に変数ではなく配列を提供しているため、あなたがしようとしている方法ではそれを行うことはできません。次の 2 つの方法を使用できます。

  1. 2つの配列nameを作成dobし、同じインデックスにそれぞれレコードを追加して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 に答える