Question: I ' m using the chinook database. For some reason, the error message for failing to connect always defaults to print out that it opened

I'm using the chinook database. For some reason, the error message for failing to connect always defaults to print out that it opened successfully. Also, I think something is wrong with my sql statement because it sometimes prints double of some tracks and will also print some tracks that don't exist. Here is a description of the problem. Write a C++ program that opens a connection to the Chinook database chinook.db Download chinook.db. If the connection doesn't open successfully print an error message ("Error in connection: unable to open database file") from the database and end the program. Print a success message when it opens successfully. Then use sqlite3_exec and a callback function (which may require more than one callback function) to print the albums in the database. For each album, there should be a track listing. Each album will appear in the results only once. After running the query close the database to prevent corrupting it. Here is my code: "/* Program name: jbtictactoe.cpp
* Author: John Doe
* Date last updated: 5/1/2017
* Purpose: Play the game of Tic-Tac-Toe
*/
#include
#include
#include "sqlite3.h"
// Callback function to print album details
int callback(void *data, int argc, char **argv, char **azColName){
// Extract album details
std::cout << "AlbumId: "<< argv[0]<< std::endl;
std::cout << "Album Name: "<< argv[1]<< std::endl;
std::cout << "Artist Name: "<< argv[2]<< std::endl;
// Extract and print track listing
std::cout << "Tracks:" << std::endl;
std::cout << argv[3]<< std::endl;
std::cout << std::endl;
return 0;
}
int main(){
sqlite3*db;
char *zErrMsg = nullptr;
int rc;
// Open connection to the database
rc = sqlite3_open("chinook.db", &db);
if (rc != SQLITE_OK){
std::cerr << "Error in connection: "<< sqlite3_errmsg(db)<< std::endl;
sqlite3_close(db);
return -1;
}
else{
std::cout << "Database opened successfully" << std::endl;
}
// Query to retrieve albums and their tracks
const char *sql = "SELECT albums.AlbumId, albums.Title, artists.Name, group_concat(tracks.Name, '
')\
FROM albums \
INNER JOIN artists ON albums.ArtistId = artists.ArtistId \
INNER JOIN tracks ON albums.AlbumId = tracks.AlbumId \
GROUP BY albums.AlbumId";
// Execute the query
rc = sqlite3_exec(db, sql, callback, nullptr, &zErrMsg);
// Close the database connection
sqlite3_close(db);
return 0;
}"

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!