2012-03-14 86 views
0

我是新来使用sqlite和iPhone,我有一些麻烦添加一条记录到我的数据库。我已经使用了我可以在线的方式尝试解决这个问题,但我陷入了一种尴尬的境地。插入数据到iPhone上的sqlite数据库 - 不工作

我使用这段代码试图将记录添加到数据库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); 
    } 
} 

警报视图“不添加产品”总是出现,任何人都可以解释为什么我的纪录ISN” t被添加?

感谢,

杰克

+0

你可以使用'sqlite3_errmsg(数据库)'来澄清问题是什么? – FluffulousChimp 2012-03-14 17:29:37

+0

Dupe of http://stackoverflow.com/questions/2785211/sqlite3-database-doesnt-actually-insert-data-iphone – MishieMoo 2012-03-14 17:39:32

回答

2

它,因为你是直接在folder.it不可写的资源将数据写入到database首先你需要在databasedocument directory复制。

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; 
        }   
} 
1

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

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

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

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

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

而且,你不要,如果你想将其绑定到该语句需要每一个数值转换为字符串。你可以使用sqlite3_bind_int,sqlite3_bind_double等。