Question: I ' m using the sakila database. I need help with my viewCustomer function ( listed below ) . Here is a description for what

I'm using the sakila database. I need help with my viewCustomer function (listed below). Here is a description for what it needs to do: This function should run a query that gets the customer information for all customers in the database and uses that to display a menu of options containing the id - last name, first name to the user. Based on the user choice the function should print the customer's name, phone number, and address including city, email address, if they are active, and last update for the record. You should use prepared statements to run the queries. Here is the code: "void viewCustomer(sqlite3* db)
{
string query = "SELECT customer_id, last_name, first_name FROM customer";
sqlite3_stmt* stmt;
int rc = sqlite3_prepare_v2(db, query.c_str(),-1, &stmt, NULL);
if (rc != SQLITE_OK){
cerr << "Error preparing statement: "<< sqlite3_errmsg(db)<< endl;
sqlite3_close(db);
exit(1);
}
cout << "Please choose the customer you want to see:" << endl;
int pageNum =1;
int rowsPerPage =10;
int choice;
while (true){
// Execute the prepared statement
for (int i =0; i < rowsPerPage; ++i){
if (sqlite3_step(stmt)== SQLITE_ROW){
cout << pageNum * rowsPerPage + i <<".";
cout << sqlite3_column_int(stmt,0)<<"-";
cout << sqlite3_column_text(stmt,2)<<"";
cout << sqlite3_column_text(stmt,1)<< endl;
} else {
// No more rows
break;
}
}
cout << "Please choose the customer you want to see (enter 0 to go to the next page";
cout <<" or -1 to go to the previous page): ";
cin >> choice;
if (choice ==0){
pageNum++;
} else if (choice ==-1){
pageNum = max(1, pageNum -1);
} else {
// Invalid choice
cout << "Invalid choice. Please try again." << endl;
}
}
sqlite3_finalize(stmt);
}"

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!