-5

iPhoneのsqliteデータベースにカスタムオブジェクトを挿入しようとしていますが、これを行う方法を教えてください。可能であれば、簡単に理解できるようにソースコードを教えてください。事前に感謝します

4

1 に答える 1

2

挿入にこのコードを使用するのはデータベースです....要件に応じてコードを変更します...私にとってはうまくいきます...

-(void) insertDataIntoDatabase
{
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

    NSString *documentsDir = [documentPaths objectAtIndex:0];

    databasePath = [documentsDir stringByAppendingPathComponent:@"dataBaseName.sqlite"];

    NSLog(@"%@",sender);

    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
    {
        NSLog(@"%@",[quoteDic objectForKey:@"bookmark"]);
        NSLog(@"%d",[[quoteDic objectForKey:@"quoteId"] intValue]);
        NSString *sqlStatement=[NSString stringWithFormat:@"insert into tableName (frndName,frndDescription,frndAddress,frndMobile,frndEmail,frndCollege,frndCompany) Values('%@','%@','%@','%@','%@','%@','%@')",[frndName text],[frndDescription text],[frndAddress text],[frndMobile text],[frndEmail text],[frndCollege text],[frndCompany text]];

        sqlite3_stmt *compiledStatement;
        if(sqlite3_prepare_v2(database, [sqlStatement UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK)
        {
        }
        if(sqlite3_step(compiledStatement) != SQLITE_DONE)
        {

            //sqlite3_close(database);
            NSAssert1(0, @"Error while Updating data. '%s'", sqlite3_errmsg(database));
            sqlite3_finalize(compiledStatement);
        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Update!!"
                                                            message:@"Your record Updated"
                                                           delegate:nil
                                                  cancelButtonTitle:@"Thankyou"
                                                  otherButtonTitles:nil];
            [alert show];
            [alert release];

        }

        sqlite3_finalize(compiledStatement);
    }
    sqlite3_close(database);
}
于 2012-06-15T09:23:28.597 に答える