0

私は問題を理解することができません。

- (void)viewDidLoad {
    NSString *docsDir;
    NSArray *dirPaths;

    // Get the documents directory
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    docsDir = [dirPaths objectAtIndex:0];

    // Build the path to the database file
    databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"contacts.sqlite"]];
}   

- (IBAction) saveData{
    sqlite3_stmt    *statement;

    const char *dbpath = [databasePath UTF8String];
    if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
    {
        NSLog(@"enter %@",name.text);

        NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO EMP (name) VALUES (\"%@\")", name.text];

        //NSLog(@"enter123 %@",dbpath);

        const char *insert_stmt = [insertSQL UTF8String];

        sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL);
        NSLog(@"enter789 ");

        if (sqlite3_step(statement) == SQLITE_DONE)
        {
            NSLog(@"Done success");

            /*status.text = @"Contact added";
            name.text = @"";
            address.text = @"";
            phone.text = @"";*/
        } 
        else
        {
            NSLog(@"fail");
            //status.text = @"Failed to add contact";
        }
        sqlite3_finalize(statement);
        sqlite3_close(contactDB);
    }
}
4

1 に答える 1

-1

これを使えば助かります

 sqlite3_stmt *statement;
    const char *dbpath = [databasePath UTF8String];
    if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK){
    const char *insert_stmt = "INSERT INTO EMP (name) VALUES (?)";
    if(sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL)==SQLITE_OK){
       sqlite3_bind_text( statement,1, [name.text UTF8String], -1, SQLITE_TRANSIENT);
    }
    if (sqlite3_step(statement) == SQLITE_DONE)
        NSLog(@"You have Sucessfully saved!");
    else
        NSLog(@"Failed to Save!");

       sqlite3_finalize(statement);
    }
于 2014-01-13T11:32:24.650 に答える