Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- http://www.codingfriends.com/index.php/2010/02/17/mysql-connection-example/
- To compile up this program you will need to link to the mysql libraries and headers that
- * are used within the program, e.g. mysql.h at the top of the program. To gain access to these, there is a nice mysql_config
- * (you may need to install it via your package manager system if you do not have it already).
- Here are my outputs of what is required on the command line for the g++ compiler
- g++ -I/usr/include/mysql winedb.cpp -o winedb -L/usr/lib/mysql -lmysqlclient
- ./windb "select * from wineInfo where price > 100"
- */
- #include <mysql/mysql.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <iostream>
- #include <iomanip>
- #include <sstream>
- #include "dbconnect.h"
- #include "LinkedList.hpp"
- using namespace std;
- /*
- argv[1] - put sql command in argv[1], otherwise, just
- use sql "show tables"
- */
- int max;
- int min;
- int option;
- int main(int argc, char* argv[])
- {
- List<Wine> wineList;
- MYSQL *conn; // the connection
- MYSQL_RES *res; // the results
- MYSQL_ROW row; // the results row (line by line)
- struct connection_details mysqlD;
- mysqlD.server = (char *)"localhost"; // where the mysql database is
- mysqlD.user = (char *)"root"; // the root user of mysql
- mysqlD.password = (char *)"password"; // the password of the root user in mysql
- mysqlD.database = (char *)"mysql"; // the databse to pick
- // connect to the mysql database
- conn = mysql_connection_setup(mysqlD);
- //~ cout << "Hello, Would you like to search by Price(1), Score(2), or year(3)"<<endl;
- //~ cin >> option;
- //~ }
- // assign the results return to the MYSQL_RES pointer
- if (argc < 2)
- {
- cout << "argv[0]: " << argv[0] << endl;
- printf("\nUsage: %s \"SQL statement here\"\n", argv[0]);
- printf("if no argument given, default is:\n %s show tables\n", argv[0]);
- res = mysql_perform_query(conn, (char *)"show tables");
- printf("MySQL Tables in mysql database:\n");
- }
- //~ if (option ==1)
- //~ {
- //~ cout << "argv[0]: " << argv[0] << endl;
- //~ printf("\nUsage: %s \"SQL statement here\"\n", argv[0]);
- //~ printf("if no argument given, default is:\n %s show tables\n", argv[0]);
- //~ res = mysql_perform_query(conn, (char *)"show tables");
- //~ printf("MySQL Tables in mysql database:\n");
- //~ cout << "What is Your max price? and What is your lowest you would like to see?" << endl;
- //~ cin >> x;
- //~ cin >> y;
- //~ std::ostringstream strstream;
- //~ strstream << "select * from wineInfo where price <= " << x << " and price >= " << y << ";";
- //~ const std::string& str = strstream.str();
- //~ const char* chrr = str.c_str();
- //~ }
- if (option ==2)
- {
- //~ cout << "What is Your max price? and What is your lowest you would like to see?" << endl;
- //~ cin >> x;
- //~ cin >> y;
- std::ostringstream strstream;
- //~ strstream << "select * from wineInfo where price <= " << x << " and price >= " << y << ";";
- strstream << "select * from wineInfo where vintage >= 2012 and vintage <= 2013 and price < 100 and price > 50 order by vintage desc;";
- const std::string& str = strstream.str();
- const char* chrr = str.c_str();
- res = mysql_perform_query(conn, (char*) chrr);
- //~ cout << "What is the max Score? and what is the lowest score you would like to see?"<<endl;
- //~ cin >> x;
- //~ cin >> y;
- //~ // res = mysql_perform_query(conn, (char *)"select * from wineInfo where score >= "<< x << "and score <=" << y <<";order by score desc;");
- }
- //~ if (option ==3)
- //~ {
- //~ cout << "What is the Highest Year you would like to see? What is the lowest?" << endl;
- //~ cin >> x;
- //~ cin >> y;
- //~ res = mysql_perform_query(conn, (char *)"select * from wineInfo where vintage >= "<< x << "and vintage <=" << y <<";order by vintage desc;");
- //~ }
- else
- {
- // use wine database
- res = mysql_perform_query(conn, (char *)"use wine");
- cout << "argv[0]: " << argv[0] << endl;
- cout << "argv[1]: " << argv[1] << endl;
- res = mysql_perform_query(conn, argv[1]);
- /*
- * you need to print out the header. Make sure it it
- * nicely formated line up. Modify the cout statement
- * below so the header is nicely line up. Hint: use left and setw
- *
- * WineName Vitange Rating Price Type
- * */
- cout << "Wine Name "
- << "Vintage"
- << " Rating"
- << " Price"
- << " Type"
- << endl;
- }
- int key=0;
- while ((row = mysql_fetch_row(res)) !=NULL)
- {
- if (argc < 2) {
- printf("%s\n", row[0]); // only print out 1st column
- }
- else
- {
- /* print out each row of the data extracted from
- * MySQL database
- * Make sure the output is line up with the header
- * Hint: use left and setw
- */
- cout << row[0] << " " // coulumn (field) #1 - Wine Name
- << row[1] << " " // field #2 - Vintage
- << row[2] << " " // field #3 - Rating
- << row[3] << " " // field #4 - Price
- << row[4] << " " // field #5 - Wine type
- << endl; // field #7 - UPC
- wineList.insertAtBack(Wine(row[0],atoi(row[1]),atoi(row[2]),atof(row[3]),row[4]),key++);
- }
- }
- /* clean up the database result set */
- mysql_free_result(res);
- /* clean up the database link */
- mysql_close(conn);
- return 0;
- }
- //~ if (argc < 2)
- //~ {
- //~ cout << "argv[0]: " << argv[0] << endl;
- //~ printf("\nUsage: %s \"SQL statement here\"\n", argv[0]);
- //~ printf("if no argument given, default is:\n %s show tables\n", argv[0]);
- //~ res = mysql_perform_query(conn, (char *)"show tables");
- //~ printf("MySQL Tables in mysql database:\n");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement