//example of C++ database access by Lin Jensen 2006, updated November 2020 #include #include using namespace std; /* * to compile: g++ -lpq fruitstand.cc */ //#include #include #include #include #define CONNINFO "dbname=fruitstand" 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) { string fruit; /*varchar(12), and \0 */ string 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); cout << "Fruit Quantity (^D for done): \n"; cin >> fruit >> qty; while(cin.good() ) { cout << "You want " << qty <<" " << fruit << "s -- "; fvalues[0]=fruit.c_str(); fvalues[1]=qty.c_str(); res = PQexecPrepared(conn,"ordins", 2, fvalues, NULL,NULL,0); if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "INSERT failed: %s", PQresultErrorMessage(res)); } else { puts("row inserted"); } PQclear(res); cin >> fruit >> qty; // next input (or ^D) } /* end while - on break */ } // end askfruits /*--------------------------*/ void displayorder(PGconn *conn, char *saleid) { PGresult *res; const char *paramValues[1]; int row, col; paramValues[0] = saleid; cout << "----- Your Order -----\n"; 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 ---*/ cout<< "Customer : "<< PQgetvalue(res, 0, PQfnumber(res, "who"))<