Advertisement
Guest User

afdafdfaa

a guest
May 11th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.76 KB | None | 0 0
  1. /*
  2.  
  3. http://www.codingfriends.com/index.php/2010/02/17/mysql-connection-example/
  4.  
  5. To compile up this program you will need to link to the mysql libraries and headers that
  6. * 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
  7. * (you may need to install it via your package manager system if you do not have it already).
  8.  
  9. Here are my outputs of what is required on the command line for the g++ compiler
  10.  
  11.  
  12.  
  13. g++ -I/usr/include/mysql winedb.cpp -o winedb -L/usr/lib/mysql -lmysqlclient
  14.  
  15.  
  16. ./windb "select * from wineInfo where price > 100"
  17.  
  18. */
  19.  
  20. #include <mysql/mysql.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <iostream>
  24. #include <iomanip>
  25. #include <sstream>
  26. #include "dbconnect.h"
  27. #include "LinkedList.hpp"
  28. using namespace std;
  29.  
  30. /*
  31.  argv[1] - put sql command in argv[1], otherwise, just
  32.  use sql "show tables"
  33. */
  34. int max;
  35. int min;
  36. int option;
  37. int main(int argc, char* argv[])
  38. {
  39.  
  40.     List<Wine> wineList;
  41.   MYSQL *conn;      // the connection
  42.   MYSQL_RES *res;   // the results
  43.   MYSQL_ROW row;    // the results row (line by line)
  44.  
  45.   struct connection_details mysqlD;
  46.   mysqlD.server = (char *)"localhost";  // where the mysql database is
  47.   mysqlD.user = (char *)"root";     // the root user of mysql  
  48.   mysqlD.password = (char *)"password"; // the password of the root user in mysql
  49.   mysqlD.database = (char *)"mysql";    // the databse to pick
  50.  
  51.   // connect to the mysql database
  52.   conn = mysql_connection_setup(mysqlD);
  53.     //~ cout << "Hello, Would you like to search by Price(1), Score(2), or year(3)"<<endl;
  54.     //~ cin >> option;
  55. //~ }
  56.   // assign the results return to the MYSQL_RES pointer
  57.  
  58.  
  59.  
  60.  if (argc < 2)
  61.   {
  62.      cout << "argv[0]: " << argv[0] << endl;
  63.      printf("\nUsage: %s  \"SQL statement here\"\n", argv[0]);
  64.      printf("if no argument given, default is:\n %s show tables\n", argv[0]);
  65.      res = mysql_perform_query(conn, (char *)"show tables");
  66.      printf("MySQL Tables in mysql database:\n");
  67.  
  68. }
  69.   //~ if (option ==1)
  70.       //~ {
  71.          
  72.   //~ cout << "argv[0]: " << argv[0] << endl;
  73.      //~ printf("\nUsage: %s  \"SQL statement here\"\n", argv[0]);
  74.      //~ printf("if no argument given, default is:\n %s show tables\n", argv[0]);
  75.      //~ res = mysql_perform_query(conn, (char *)"show tables");
  76.      //~ printf("MySQL Tables in mysql database:\n");
  77.  
  78.           //~ cout << "What is Your max price? and What is your lowest you would like to see?" << endl;
  79.           //~ cin >> x;
  80.           //~ cin >> y;
  81.           //~ std::ostringstream strstream;
  82.           //~ strstream << "select * from wineInfo where price <= " <<  x  << " and price >= " << y <<  ";";
  83.           //~ const std::string& str = strstream.str();
  84.           //~ const char* chrr = str.c_str();
  85.        
  86.       //~ }
  87.   if (option ==2)
  88.   {
  89.      
  90.           //~ cout << "What is Your max price? and What is your lowest you would like to see?" << endl;
  91.           //~ cin >> x;
  92.           //~ cin >> y;
  93.           std::ostringstream strstream;
  94.           //~ strstream << "select * from wineInfo where price <= " <<  x  << " and price >= " << y <<  ";";
  95.           strstream << "select * from wineInfo where vintage >= 2012 and vintage <= 2013 and price < 100 and price > 50 order by vintage desc;";
  96.           const std::string& str = strstream.str();
  97.           const char* chrr = str.c_str();
  98.           res = mysql_perform_query(conn, (char*) chrr);     
  99.      
  100.       //~ cout << "What is the max Score? and what is the lowest score you would like to see?"<<endl;
  101.       //~ cin >> x;
  102.       //~ cin >> y;
  103.      //~ // res = mysql_perform_query(conn, (char *)"select * from wineInfo where score >= "<< x << "and score <=" << y <<";order by score desc;");
  104.   }
  105.   //~ if (option ==3)
  106.   //~ {
  107.       //~ cout << "What is the Highest Year you would like to see? What is the lowest?" << endl;
  108.       //~ cin >> x;
  109.       //~ cin >> y;
  110.       //~ res = mysql_perform_query(conn, (char *)"select * from wineInfo where vintage >= "<< x << "and vintage <=" << y <<";order by vintage desc;");
  111.   //~ }
  112.  
  113.   else
  114.   {
  115.      // use wine database
  116.      res = mysql_perform_query(conn, (char *)"use wine");
  117.      cout << "argv[0]: " << argv[0] << endl;
  118.      cout << "argv[1]: " << argv[1] << endl;
  119.      res = mysql_perform_query(conn, argv[1]);
  120.      /*
  121.       * you need to print out the header.  Make sure it it
  122.       * nicely formated line up.  Modify the cout statement
  123.       * below so the header is nicely line up.  Hint: use left and setw
  124.       *
  125.       * WineName   Vitange  Rating  Price  Type
  126.       * */
  127.      cout << "Wine Name "
  128.                        << "Vintage"
  129.                         << " Rating"
  130.                         << " Price"
  131.                         << " Type"
  132.      << endl;
  133.   }
  134. int key=0;
  135.   while ((row = mysql_fetch_row(res)) !=NULL)
  136.   {
  137.       if (argc < 2) {
  138.          printf("%s\n", row[0]);  // only print out 1st column
  139.       }
  140.       else
  141.       {
  142.           /* print out each row of the data extracted from
  143.            * MySQL database
  144.            * Make sure the output is line up with the header
  145.            * Hint: use left and setw
  146.            */
  147.  
  148.      cout << row[0]  << " " // coulumn (field) #1 - Wine Name
  149.       << row[1] << "  " // field #2 - Vintage
  150.       << row[2] << "  " // field #3 - Rating
  151.      << row[3] << "  " // field #4 - Price
  152.       << row[4] << "  " // field #5 - Wine type
  153.       << endl; // field #7 - UPC
  154.      
  155.       wineList.insertAtBack(Wine(row[0],atoi(row[1]),atoi(row[2]),atof(row[3]),row[4]),key++);
  156.       }
  157.   }
  158.   /* clean up the database result set */
  159.   mysql_free_result(res);
  160.   /* clean up the database link */
  161.   mysql_close(conn);
  162.  
  163.   return 0;
  164. }
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  //~ if (argc < 2)
  171.   //~ {
  172.      //~ cout << "argv[0]: " << argv[0] << endl;
  173.      //~ printf("\nUsage: %s  \"SQL statement here\"\n", argv[0]);
  174.      //~ printf("if no argument given, default is:\n %s show tables\n", argv[0]);
  175.      //~ res = mysql_perform_query(conn, (char *)"show tables");
  176.      //~ printf("MySQL Tables in mysql database:\n");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement