database - 需要帮助插入数据库

标签 database xcode sqlite

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

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

    }
}

谢谢 guyz 我知道了...编辑上面的代码现在它运行并在数据库中提交数据..

最佳答案

您将值绑定(bind)到准备好的语句,但看起来您从未调用过提交。

关于database - 需要帮助插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5950101/

相关文章:

iOS : How to override the button action of UIWebView and call our own Objective C method

mysql - 如何使用命令提示符检查 mysql 中每个数据库/表使用的内存?

ios - 如何在 XCode 中创建 MongoID

ios - URLSession dataTask 特定 URL 处的符号断点

c - 在 C 程序关闭和重新启动之间保存和检索简单数据的常见方法是什么?

c# - SQ-Lite 未在 Visual Studio 2015 中显示为数据提供程序

haskell - 使用 Haskell/Persistent 连接到 Sqlite3 数据库

mysql - 如何通过将 varchar 转换为 float 来求和 varchar 数据类型

database - 在 Laravel 4 中构建 SAAS 的正确方法

node.js - 为我的 Node 应用程序选择正确的数据库