2012-11-13 63 views
-1

我正在学习如何使用SQLite数据库,并且在将数据存储在表中,然后在检索它并存储到新表中时遇到问题。将数据存储在一个SQLite表中,然后检索并将其存储到另一个

我已经创建我的第一表(食物类型),其执行以下操作:

CREATE TABLE FoodType(
    type_id integer, 
    Category_id integer, 
    Type text, 
    Quantity text, 
    NumberCalories text 
) 

予从该表中的图控制器检索数据和在标签中显示的数据。现在我想将数据存储到另一个表(订单),我已经为这样的创建:

CREATE TABLE Order (
    id integer NOT NULL PRIMARY KEY AUTOINCREMENT, 
    type-meal text, 
    Quantity text, 
    NumCal text 
) 

这是我想使用将数据插入到第二个表的代码:

NSString *insertSQL = [NSString stringWithFormat: 
    @"INSERT INTO Order (type-meal,Quantity,NumCal) VALUES (\"%@\",\%@\",\"%@\")",_typeLbl.text,_Quantity.text,_Number.text]; 

谢谢

+1

什么是你的问题是什么呢?你遇到了什么错误? – Perception

+0

无法添加联系人 – Fad

+0

请显示确切的错误信息。 –

回答

1

“Order”是SQL中的保留字。尝试调用其他表。

+0

谢谢你trojanfoe,我重命名为SavedD,但我没有得到价值 – Fad

+0

@Fad好了,显示实际插入的代码。 – trojanfoe

1

尝试使用属性“REPLACE INTO”而不是“INSERT INTO”

0

不能使用标识符-字符没有引用它们。

尝试type_meal"type-meal"

0

这是我所有的代码,我用它来保存在第二的tableView具有类的类型的数据,没有什么商店的数据库,但如果我确定TYPE_ID = 4要花第四类第一类的唯一:
- (IBAction为)保存数据:(ID)发送方{ @try { 如果(sqlite3_open([[AppDelegate中getDBPath] UTF8字符串],&数据库)== SQLITE_OK){

  NSString *statement= [NSString stringWithFormat: @"INSERT INTO SavedD (type_meal,Quantity,NumCal) SELECT Type,Quantity,NumberCalories FROM FoodType Where type_id=?" ]; 
     const char *sqlstatement = [statement UTF8String]; 

       if (sqlite3_prepare_v2(database, sqlstatement, -1, &compiledStatement, NULL)== SQLITE_OK) 
       { 
        NSLog(@"Data inserted Successfully"); 
        // UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"OK" message:@"Record added" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
        // [alert show]; 

        sqlite3_bind_text(compiledStatement, 1, [mealType UTF8String], -1, SQLITE_TRANSIENT); 

        sqlite3_bind_text(compiledStatement, 2, [Quantit UTF8String], -1, SQLITE_TRANSIENT); 
        sqlite3_bind_text(compiledStatement, 3, [NumofCal UTF8String], -1, SQLITE_TRANSIENT); 

        NSLog(@"%@",mealType); 
        // NSLog(@"%@",PersonId); 
        NSLog(@"%@",Quantit); 
        NSLog(@"%@",NumofCal); 

        int iResult = sqlite3_step(compiledStatement); 
        NSLog(@"%d",iResult); 
        if (SQLITE_DONE!=iResult) 
         NSAssert1(0,@"Error when inserting  %s",sqlite3_errmsg(database)); 

       } 
       else { 

        UIAlertView *AlertOK = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Data not inserted22" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 

        [AlertOK show]; 

       } 

       sqlite3_finalize(compiledStatement); 

      } 
      sqlite3_close(database); 
     } 



     @catch (NSException * e) { 
      NSLog(@"The Record already inserted"); 
     } 

}

检查成功插入数据后。 。但“mealType”的价值观,以“Quantit”和“NumofCal”是空..

相关问题