需要有关插入数据库的帮助

发布于 2024-11-05 20:15:41 字数 2316 浏览 0 评论 0原文

我是新手...我需要插入语句的帮助..我的代码如下..代码运行但它不会在数据库内添加任何内容。请任何人告诉我为什么会这样..

- (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];

    }
}

谢谢家伙,我明白了...编辑了上面的代码,现在它运行并在数据库中提交数据..

i m a newbie... I need help with inser statement .. my code is as follows.. the code runs but it does not add anything inside the database. plz can anyone suggest me why is it so..

- (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];

    }
}

THanks guyz i got it... edited the above code now it runs and submit data in database..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘿哥们儿 2024-11-12 20:15:41

您将值绑定到准备好的语句,但看起来您从未调用过 commit。

You bind the values to the prepared statement, but it does not look like you ever call commit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文