#include #include #include #include #define CONNINFO "dbname = fruitstand" //local testing static void exit_nicely(PGconn *conn) { PQfinish(conn); exit(1); } void askfruits(PGconn *conn); //prototypes void displayorder(PGconn *conn, char *saleid); int main(int argc, char **argv) { const char *conninfo; PGconn *conn; PGresult *res; int nFields; int i, j; char *saleid; // will point to string in a resultSet char id[10]; // we make a copy const char *paramValues[1]; // for queryParams /* NOTE: everything going to and from SQL is a string */ /* for our fruit input values, no spaces! */ conninfo = CONNINFO; if (argc < 2) {printf("USAGE: %s 'Cust Name'\n", argv[0]); exit(1); } /* Make a connection to the database */ conn = PQconnectdb(conninfo); /* Check to see that the backend connection was successfully made */ if (PQstatus(conn) != CONNECTION_OK) { fprintf(stderr, "Connection to database failed: %s", PQerrorMessage(conn)); exit_nicely(conn); } /* Now we will insert a row in sales for this customer, with default date and serial saleid */ paramValues[0] = argv[1]; res = PQexecParams(conn, "insert into sales values ($1) returning saleid", 1, /* one param */ NULL, /* let the backend deduce param type */ paramValues, NULL, /* don't need param lengths since text */ NULL, /* default to all text params */ 0); /* ask for text results */ if (PQresultStatus(res) != PGRES_TUPLES_OK) { fprintf(stderr, "INSERT failed: %s", PQerrorMessage(conn)); PQclear(res); exit_nicely(conn); } saleid = PQgetvalue(res, 0, 0); printf ("Sale ID = %s for %s\n", saleid, argv[1]); strncpy(id, saleid,9); // copy this for rest of sale PQclear(res); // now clear the resultSet /**** Now we shall loop asking for fruits, until we get an empty fruit inserts may fail if the fruit isn't in the database or if the quantity isn't a valid integer to SQL. Well, we'll expect this, and just keep asking (On error, IGNORE) */ askfruits(conn); displayorder(conn, id); PQfinish(conn); // close connection return 0; } /*-------------------------------------------------------*/ void askfruits(PGconn *conn) { char *fruit; /*varchar(12), and \0 */ char *qty; /* up to 4 digits */ PGresult *res; const char *fvalues[2]; res = PQprepare(conn, "ordins" , "insert into sold values($1,$2)", 2, NULL); if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "PQprepare failed: %s", PQerrorMessage(conn)); // exit_nicely(conn); } PQclear(res); puts("Fruit Quantity (^D for done): "); while(EOF != fscanf(stdin,"%ms %ms", &fruit, &qty)) { printf("You want %s %ss\n",qty,fruit); fvalues[0]=fruit; fvalues[1]=qty; res = PQexecPrepared(conn,"ordins", 2, fvalues, NULL,NULL,0); if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "INSERT failed: %s", PQresultErrorMessage(res)); // PQclear(res); //fatal to do it twice // exit_nicely(conn); } else { puts("row inserted"); } PQclear(res); free(qty); free(fruit ); } /* end while - on break */ } // end askfruits /*--------------------------*/ void displayorder(PGconn *conn, char *saleid) { PGresult *res; const char *paramValues[1]; int row, col; paramValues[0] = saleid; puts("----- Your Order -----"); res = PQexecParams(conn, "select fruit, quantity, price, price*quantity as amt, who, date, total from fruit, sold natural join sales where name=fruit and saleid=$1", 1, /* one param */ NULL, /* let the backend deduce param type */ paramValues, NULL, NULL, 0); if (PQresultStatus(res) != PGRES_TUPLES_OK) { fprintf(stderr, "SELECT failed: %s", PQerrorMessage(conn)); exit_nicely(conn); } /*--- Display customer, date, and total ---*/ printf("Customer : %s\n", PQgetvalue(res, 0, PQfnumber(res, "who"))); printf("Date : %s\n", PQgetvalue(res, 0, PQfnumber(res, "date"))); printf("Total Due: %s\n", PQgetvalue(res, 0, PQfnumber(res, "total"))); /*--- Display details for each fruit ---*/ puts ("\n\t------------------------------"); puts ("\tFruit Qty Price Amt"); puts ("\t------------------------------"); for (row=0; row