将数据存储在一个SQLite表中,然后检索并将其存储到另一个SQLite表中

时间:2012-11-13 13:20:37

标签: iphone ios objective-c sqlite

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

我创建了我的第一个表(FoodType),如下所示:

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

我在视图控制器中从此表中检索数据并在标签中显示数据。现在我想将数据存储到我创建的另一个表(Order)中:

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

由于

4 个答案:

答案 0 :(得分:1)

“Order”是SQL中的保留字。尝试将表格称为其他内容。

答案 1 :(得分:1)

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

答案 2 :(得分:0)

您无法在不引用标识符的情况下在标识符中使用-个字符。

尝试type_meal"type-meal"

答案 3 :(得分:0)

这是我用来保存第二个tableView中具有类别类型的数据的所有代码,数据库中没有任何存储但是如果我确定type_id = 4则仅采用第一类的第四种类型:
  - (IBAction)SaveData:(id)sender {     @try {         if(sqlite3_open([[AppDelegate getDBPath] UTF8String],& database)== 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”的值为空。