Нужна помощь с вставкой в ​​базу данных - PullRequest
2 голосов
/ 10 мая 2011

Я новичок ... Мне нужна помощь с оператором вставки ... мой код выглядит следующим образом ... код выполняется, но он ничего не добавляет в базу данных. плз может кто-нибудь подсказать мне, почему это так ..

- (IBAction)add:(id)Sender{

    CGPoint loc;
    loc.x = [_x.text floatValue];
    loc.y = [_y.text floatValue];
    if(loc.x != 0 || loc.y != 0 ) 
    {
    [_x endEditing:YES];
    [_y endEditing:YES];
    [_name endEditing:YES];
        NSString *date = [[NSDate alloc]init];   
        NSNumber *x = [NSNumber numberWithFloat:loc.x];
        NSNumber *y = [NSNumber numberWithFloat:loc.y];               

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsPath = [paths objectAtIndex:0];
        NSString *filePath = [documentsPath stringByAppendingPathComponent:@"cities.sqlite"];

        sqlite3 *database;

        if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK) {
            const char *sqlStatement = "insert into table (name, xLoc,yLoc,date) VALUES ( ?, ?, ?, ?)";
            sqlite3_stmt *compiledStatement;
            if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)    {
                sqlite3_bind_text( compiledStatement, 1, [_name.text UTF8String], -1, SQLITE_TRANSIENT);

                sqlite3_bind_double( compiledStatement, 2, [x doubleValue]);

                sqlite3_bind_double(compiledStatement, 3, [y doubleValue]);

                sqlite3_bind_text(compiledStatement, 4, [date UTF8String] ,-1, SQLITE_TRANSIENT);

            }

        char* errmsg;
        sqlite3_exec(database, "COMMIT", NULL, NULL, &errmsg);

            if(sqlite3_step(compiledStatement) != SQLITE_DONE ) {
                NSLog( @"Error: %s", sqlite3_errmsg(database) );
            } else {
                NSLog( @"Insert into row id = %lld", sqlite3_last_insert_rowid(database));
            }
            sqlite3_finalize(compiledStatement);
        }
        sqlite3_close(database);




        [[self navigationController]popViewControllerAnimated:YES];
    }
    else{
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Wrong Co-ordinates described." delegate:nil cancelButtonTitle:@"Go Back" otherButtonTitles:nil];
        [alert show];
        [alert release];

    }
}

Спасибо, парень! Я получил его ... отредактировал приведенный выше код, теперь он запускается и отправляет данные в базу данных.

1 Ответ

0 голосов
/ 10 мая 2011

Вы связываете значения с подготовленным оператором, но это не похоже на то, что вы когда-либо вызывали commit.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...