i have a view Controller called as VegQuantity which does totalcost=(quantity*cost of the dish) and inserts the itemname,quantity,totalcost into a table called as FINALORDER with database name 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);
}
Final View Controller viewdidload method
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);
}
But i keep getting this error called expected expression before FinalOrder,and is it correct for me to write this code inside viewdidload? i dont have any button in Final view controller i have a button called order in a view controller called as Restaurant which actually shows me Final view controller..am i supposed to search for the db file again in the Final viewcontroller and i am sorry question seems kind of vague but in brief i just want to know how to retrieve and display the data which i have inserted in VegQuantity view controller into the final view controller thanks
I believe that the problem must rest in the definition of
FinalOrderwhich, on the basis of the error message, looks like has been defined as a class, not as asqlite3 *variable. Given that the scope of your usage of the database is limited to these two methods, I’d suggest defining asqlite3 *within that scope, and use that, such as:Note, in addition to using a
sqlite3 *variable for the database, to make this a little more robust:I have replaced the
stringWithFormatstatement that built the SQL insert statement with anINSERTstatement that uses the?placeholders and then usesqlite3_bind_textto bind values to that statement. This way, if someone entered a value that included a quotation mark, the insert statement will still work (your original implementation would have crashed and/or was susceptible to a SQL injection attack).I have also added the
sqlite3_errmsgstatements so if something goes wrong, I know what the problem was.Rather than treating these three fields as text fields, I’m assuming your table is defined as
CREATE TABLE IF NOT EXISTS FINALORDER (itemname TEXT, quantity INT, totalcost REAL);and therefore use text, int, and double bind statements.This, incidentally, invokes a
purgeTablemethod, so if you run it twice, it will remove the old record in there:Anyway, you can then read this data via:
Note,
I’ve added a
sqlite3_errmsglog statement if thesqlite3_preparefails, and I’ve retired thestringWithFormat(because you weren’t formatting anything).Given that
quantitywasINTandtotalwasREAL, I’m retrieving the values using the appropriate variation ofsqlite3_column_text,sqlite3_column_int, orsqlite3_column_double, as appropriate.I infer that you’re not using ARC and therefore, the
[NSString alloc]must have an associatedreleaseorautorelease.Finally, in our chat, you said that you received an error message (presumably an “Undefined Symbols” message) that said:
This means that you are trying to use an object class of
FinalOrder, but you haven’t defined that class anywhere. Take a look at which .o files it’s reporting this error for and look at the corresponding .m file, and look for your use of theFinalOrderclass there. You clearly have aFinalOrderclass interface defined somewhere, but never defined the class implementation, or, if you have one, for some reason it’s not included in your target’s “Compile Sources” listing, so double check it’s here:Finally, by the way, make sure your database is in the
Documentsfolder, not trying to open a copy of the database in your project’s bundle (because that’s read only). If you have a copy of the database in your bundle, just check to see if you already have it in youDocumentsfolder, and if not, copy it from the bundle to theDocumentsfolder.