我有一个名为 VegQuantity 的视图控制器,它执行 totalcost=(quantity*cost of the dish) 并将 itemname、quantity、totalcost 插入名为 FINALORDER 且数据库名称为 FinalOrder 的表中
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &FinalOrder) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO FINALORDER (itemname, quantity, totalcost) VALUES (\"%@\", \"%@\", \"%@\")", itemName.text, input.text, output.text];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(FinalOrder, insert_stmt, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
// status.text = @"Contact added";
// name.text = @"";
// address.text = @"";
// phone.text = @"";
NSLog(@"added");
} else {
NSLog(@"Couldnt add");
}
sqlite3_finalize(statement);
sqlite3_close(FinalOrder);
}
最终视图控制器 viewdidload 方法
const char *dbpath = [databasePath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &FinalOrder) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat: @"SELECT * FROM FINALORDER"];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(FinalOrder, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
if (sqlite3_step(statement) == SQLITE_ROW)
{
NSString *itemname = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];
item.text = itemname;
NSString *qua = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
quantity.text = qua;
NSString *total = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)];
totalcost.text=total;
}
sqlite3_finalize(statement);
}
sqlite3_close(FinalOrder);
}
但是我在 FinalOrder 之前一直收到这个错误,称为预期表达式,我在 viewdidload 中编写这段代码是否正确?我在最终视图控制器中没有任何按钮 我在名为 Restaurant 的视图控制器中有一个名为 order 的按钮,它实际上向我显示最终视图控制器..我应该在最终视图控制器中再次搜索 db 文件,我很抱歉问题似乎有点含糊,但简而言之,我只想知道如何检索我在 VegQuantity 视图控制器中插入的数据并将其显示到最终视图控制器中,谢谢