MySQL Forums
Forum List  »  Connector/C++

Mysql c++ application help
Posted by: Rashmi Patel
Date: April 10, 2016 10:18PM

I was wondering if anyone can help me figure how to do something. I have created an C++ application with a menu that links to mySql database to pull data from. I have managed to pull data from my program to display it in c++ but the output formatting is total messed up. I have tried to make it look nice but my data doesn't seem to line up correctly.

So I was wondering if someone could tell me how to fix my formatting issue?

Another question is how to add data into mySQL database because one of the options is to add some data into a field. I have checked this online but can't seem to find exactly how to do this.

My query for option 2 and 3 don't seem to work, can you help me fix them?

My C++ program:

#include <stdio.h>
#include <mysql.h>
#include <iostream>
#include <iomanip>
using namespace std;
#define SERVER "students"
#define USER "********"
#define PASSWORD "*********"
#define DATABASE "cs566402"

int main() {

MYSQL *connect, mysql; //Creates a pointer to the MySQL instance
connect = mysql_init(&mysql); // Initialize the instance
connect = mysql_real_connect(connect, SERVER, USER, PASSWORD, DATABASE, 0, NULL, 0); //Connect to the specific database
if (connect) {
cout << "connection Succeeded\n";
} else {
cout << "connection failed\n";
}
MYSQL_RES *res_set;
MYSQL_ROW row;

cout << endl;
int option;
do {
cout << "1) List all wines that any winery produces " << endl;
cout << "2) List all wineries in a particular region " << endl;
cout << "3) List all wines with all of the grape varieties included in each wine" << endl;
cout << "4) Add a new winery (when adding a winery
there is no need for it to have any wines associated, but you must be able to put it in an existing
region)" << endl;
cout << "5) Exit Program " << endl;
cout << endl;
//Prompting user to enter an option according to menu
cout << "Please select an option : ";
cout << endl;
cin >> option; // taking option value as input and saving in variable "option"

if (option == 1) // Checking if user selected option 1
{
mysql_query(connect, "select w.WineId, w.WineName, wn.WineryName from Wine w, Winery wn where w.WineryId = wn.WineryId");
unsigned int i = 0;
res_set = mysql_store_result(connect);
unsigned int numrows = mysql_num_rows(res_set);

cout << setfill('-') << setw(1) << "+" << setw(11) << "-" << setw(1) << "+" << setw(45) << "-" << setw(1) << "+" << setw(30) << "-" << setw(1) << "+" << endl;
cout << setfill(' ') << setw(31) << left << "WineId" << setw(33) << left << "WineName" << setw(24) << left << "WineryName" << endl;
cout << setfill('-') << setw(1) << "+" << setw(11) << "-" << setw(1) << "+" << setw(45) << "-" << setw(1) << "+" << setw(30) << "-" << setw(1) << "+" << endl;

while (((row = mysql_fetch_row(res_set)) != NULL)) { //cout<<" %s\n",row !=NULL?row : "NULL";

cout << setw(1) << left << row << " \t \t" << setw(31) << row[i + 1] << " \t \t" << setw(11) << right << row[i + 2] << endl;
}
} else if (option == 2) // Checking if user selected option 2
{
mysql_query(connect, "select WineryName, Region from Winery");
unsigned int i = 0;
res_set = mysql_store_result(connect);
unsigned int numrows = mysql_num_rows(res_set);
while (((row = mysql_fetch_row(res_set)) != NULL)) { //cout<<" %s\n",row !=NULL?row : "NULL";

cout << "|" << row << "| \t";
cout << row[i + 1] << endl;
}
} else if (option == 3) // Checking if user selected option 3
{
mysql_query(connect, "select g.GrapeType, w.WineName from GrapeType g, Wine w, Varieties v where g.GrapeId = v.GrapeId and v.WineId = w.WineId");
unsigned int i = 0;
res_set = mysql_store_result(connect);
unsigned int numrows = mysql_num_rows(res_set);
while (((row = mysql_fetch_row(res_set)) != NULL)) { //cout<<" %s\n",row !=NULL?row : "NULL";

cout << "|" << row << "| \t";
cout << row[i + 1] << endl;
}
} else if (option == 4) // Checking if user selected option 3
{

} else if (option == 5) // Checking if user selected option 4
{
cout << "Terminating Program" << endl;
} else //if user has entered invalid choice (other than 1,2,3,4, or 5)
{
//Displaying error message
cout << "Invalid Option entered" << endl;
}
} while (option != 5); //condition of do-while loop

//return 0;



mysql_close(connect);
return 0;
}

Options: ReplyQuote


Subject
Views
Written By
Posted
Mysql c++ application help
1933
April 10, 2016 10:18PM
934
April 10, 2016 10:37PM
807
April 14, 2016 04:00AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.