Guest User

winedb

a guest
May 10th, 2017
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 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 "dbconnect.h"
  26.  
  27. using namespace std;
  28.  
  29.  
  30.  
  31.  
  32. /*
  33. argv[1] - put sql command in argv[1], otherwise, just
  34. use sql "show tables"
  35. */
  36. int main(int argc, char* argv[])
  37. {
  38. MYSQL *conn; // the connection
  39. MYSQL_RES *res; // the results
  40. MYSQL_ROW row; // the results row (line by line)
  41.  
  42. struct connection_details mysqlD;
  43. mysqlD.server = (char *)"localhost"; // where the mysql database is
  44. mysqlD.user = (char *)"root"; // the root user of mysql
  45. mysqlD.password = (char *)"password"; // the password of the root user in mysql
  46. mysqlD.database = (char *)"mysql"; // the databse to pick
  47.  
  48. // connect to the mysql database
  49. conn = mysql_connection_setup(mysqlD);
  50.  
  51. //~ }
  52.  
  53. // assign the results return to the MYSQL_RES pointer
  54. if (argc < 2)
  55. {
  56. cout << "argv[0]: " << argv[0] << endl;
  57. printf("\nUsage: %s \"SQL statement here\"\n", argv[0]);
  58. printf("if no argument given, default is:\n %s show tables\n", argv[0]);
  59. res = mysql_perform_query(conn, (char *)"show tables");
  60. printf("MySQL Tables in mysql database:\n");
  61. }
  62. else
  63. {
  64. // use wine database
  65. res = mysql_perform_query(conn, (char *)"use wine");
  66. cout << "argv[0]: " << argv[0] << endl;
  67. cout << "argv[1]: " << argv[1] << endl;
  68. res = mysql_perform_query(conn, argv[1]);
  69. /*
  70. * you need to print out the header. Make sure it it
  71. * nicely formated line up. Modify the cout statement
  72. * below so the header is nicely line up. Hint: use left and setw
  73. *
  74. * WineName Vitange Rating Price Type
  75. * */
  76. cout << "Wine Name "
  77. << "Vintage"
  78. << " Rating"
  79. << "Price"
  80. << "Type"
  81. << endl;
  82. }
  83.  
  84. while ((row = mysql_fetch_row(res)) !=NULL)
  85. {
  86. if (argc < 2) {
  87. printf("%s\n", row[0]); // only print out 1st column
  88. }
  89. else
  90. {
  91. /* print out each row of the data extracted from
  92. * MySQL database
  93. * Make sure the output is line up with the header
  94. * Hint: use left and setw
  95. */
  96.  
  97. cout << row[0] << " " // coulumn (field) #1 - Wine Name
  98. << row[1] << " " // field #2 - Vintage
  99. << row[2] << " " // field #3 - Rating
  100. << row[3] << " " // field #4 - Price
  101. << row[4] << " " // field #5 - Wine type
  102. << endl; // field #7 - UPC
  103. }
  104. }
  105. /* clean up the database result set */
  106. mysql_free_result(res);
  107. /* clean up the database link */
  108. mysql_close(conn);
  109.  
  110. return 0;
  111. }
Add Comment
Please, Sign In to add comment