【问题标题】:Inserting Data into sqlite database on iPhone - not working将数据插入 iPhone 上的 sqlite 数据库 - 不工作
【发布时间】:2012-03-14 17:14:49
【问题描述】:

我是使用 sqlite 和 iPhone 的新手,在向我的数据库添加记录时遇到了一些麻烦。我已经在网上尝试解决这个问题,但我陷入了困境。

我正在使用这段代码尝试在 sqlite 中将记录添加到数据库中:

    -(void)insertData{

    sqlite3 *database;
    sqlite3_stmt *statement;
    NSString *databasePath = [[NSBundle mainBundle] pathForResource:@"ProductDatabase" ofType:@"sql"];
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
    {
        //nutrional values
        float caloriesFloat = [caloriesTextField.text floatValue];
        float saturatesFloat = [saturatesTextField.text floatValue];
        float fatFloat = [fatTextField.text floatValue];
        float fibreFloat = [fibreTextField.text floatValue];
        float sugarFloat = [sugarTextField.text floatValue];
        float saltFloat = [saltTextField.text floatValue];

        NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO products (name,category,calories, saturates,fat,fibre,sugar,salt) VALUES ('%@',' %@','%.02f','%.02f','%.02f','%.02f','%.02f','%.02f',);",nameTextField.text, categoryLabel.text, caloriesFloat, saturatesFloat, fatFloat, fibreFloat, sugarFloat, saltFloat];
        const char *insert_stmt = [insertSQL UTF8String];
        if(sqlite3_prepare_v2(database, insert_stmt, -1, &statement, nil)== SQLITE_OK)
        {
            sqlite3_bind_text(statement, 1, [nameTextField.text UTF8String], -1, NULL);
            sqlite3_bind_text(statement, 2, [categoryLabel.text UTF8String], -1, NULL);
            sqlite3_bind_text(statement, 3, [[NSString stringWithFormat:@"%f", caloriesFloat] UTF8String], -1, NULL);
            sqlite3_bind_text(statement, 3, [[NSString stringWithFormat:@"%f", saturatesFloat] UTF8String], -1, NULL);
            sqlite3_bind_text(statement, 3, [[NSString stringWithFormat:@"%f", fatFloat] UTF8String], -1, NULL);
            sqlite3_bind_text(statement, 3, [[NSString stringWithFormat:@"%f", fibreFloat] UTF8String], -1, NULL);
            sqlite3_bind_text(statement, 3, [[NSString stringWithFormat:@"%f", sugarFloat] UTF8String], -1, NULL);
            sqlite3_bind_text(statement, 3, [[NSString stringWithFormat:@"%f", saltFloat] UTF8String], -1, NULL);
        }

        if(sqlite3_step(statement)==SQLITE_DONE) 
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Product Added" message:@"" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];    
            [alert show];

        }
        else 
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Product Not Added" message:@"An error has occured" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];   
            [alert show];
            alert=nil;
        }   
        // Release the compiled statement from memory
        sqlite3_finalize(statement);    
        sqlite3_close(database);
    }
}

总是出现“未添加产品”的警报视图,谁能解释为什么我的记录没有被添加?

谢谢,

杰克

【问题讨论】:

标签: iphone objective-c database sqlite


【解决方案1】:

因为你是直接将数据写入资源文件夹中的database。它是不可写的,首先你需要复制document directory中的database

NSString *databasePath=[[NSBundle mainBundle]pathForResource:@"ProductDatabase" ofType:@"sql"];

在上面一行之后添加下面写的代码

NSError *err=nil;
    NSFileManager *fm=[NSFileManager defaultManager];

    NSArray *arrPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, -1);
    NSString *path=[arrPaths objectAtIndex:0];
   NSString path2= [path stringByAppendingPathComponent:@"ProductDatabase.sql"];


   if(![fm fileExistsAtPath:path2])
    {

      bool success=[fm copyItemAtPath:databasePath toPath:path2 error:&err];
        if(success)
            NSLog(@"file copied successfully");
       else
           NSLog(@"file not copied");

    }

并从代码中删除绑定语句并将其更改为

if(sqlite3_prepare_v2(database, insert_stmt, -1, &statement, nil)== SQLITE_OK)
        {
           

        if(sqlite3_step(statement)==SQLITE_DONE) 
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Product Added" message:@"" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];    
            [alert show];

        }
        else 
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Product Not Added" message:@"An error has occured" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];   
            [alert show];
            alert=nil;
        }   
}

【讨论】:

    【解决方案2】:

    首先,将您的数据库复制到文档中。您不能在主包中修改它。

    其次,在sqlite3数据库上创建和执行sql语句有两种方式。

    一种方法是使用字符串操作(我们 %@、%d 等用于添加字符串和数值)创建整个 sql 字符串并在 db 上执行 sql。

    另一种方法是为 sql 字符串中的每个变量添加一个问号 (?),并将一个变量绑定到每个变量?使用 sqlite3_bind 语句

    每种方法都有其优点和缺点,但您可以选择您想要的一种。 你的代码都错了

    此外,如果要将每个数值绑定到语句,则无需将其转换为字符串。您可以使用 sqlite3_bind_int、sqlite3_bind_double 等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-10
      • 1970-01-01
      • 1970-01-01
      • 2018-11-24
      • 1970-01-01
      • 2015-01-24
      • 1970-01-01
      相关资源
      最近更新 更多