2012-03-14 98 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等。