Advertisement
Guest User

Untitled

a guest
Aug 24th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. /*
  2. -SheldonJ22, software provided as is. Feel free to use, modify and redistribute without restriction.
  3. mysql.exe Version 1.0
  4. Tested on Win10 and Win Srv 2012R2 with MariaDB
  5. */
  6. #include <stdio.h>
  7. #include "include/mysql.h"
  8.  
  9. void print_result_csv(MYSQL_RES *result);
  10. void print_result_json(MYSQL_RES *result);
  11.  
  12. int main(int argc, char *argv[]) {
  13. MYSQL *mysql_connection;
  14. MYSQL_RES *mysql_result;
  15.  
  16. mysql_connection = mysql_init(NULL);
  17. char *user;
  18. char *pass;
  19. char *database;
  20. char *server;
  21. char *query;
  22. int i=1;
  23. char print_args=0;//!0 prints user arguements
  24. char print_help=0;//!0 prints help
  25. char print_json=0;//!0 prints json
  26. FILE *std_err = stderr;//output stream - can be set to stdout by user
  27. char *help_message="\nCommand line usage\n \
  28. -a [optional] print the argumentst that you entered\n \
  29. -d [required] the database you will mysql_connectionect to\n \
  30. -h [optional] print this help file\n \
  31. -j [optional] print output in json format\n \
  32. -o [optional] redirect stderr to stdout\n \
  33. -p [required] password for user to mysql_connectionect to database\n \
  34. -q [required] the query to send\n \
  35. -s [required] server to mysql_connectionect to , IP or hostname\n \
  36. -u [required] username for database\n \
  37. \n \
  38. Example usage:\n \
  39. mysql.exe -uusername -ppassword -ddatabasename -s\"1.2.3.4\" -q\"select * from database.table where field='value'\"\n \
  40. Output:\n \
  41. default output displays in csv format, 1 row for each line and fields seperated by commas. If you specifiy -j it will display an array in json format on 1 line\n \
  42. Json format: [{\"0\":\"value\",\"1\",\"value\"},{\"0\":\"value\",\"1\",\"value\"}]\n \
  43. Troubleshooting\n \
  44. Be sure to have libmysql.dll in same directory as execution\n \
  45. Be sure you have given proper access on the DB server\n \
  46. Be sure you have opened up necessary ports on DB server\n \
  47. \n\n";
  48. for(i;i<argc && argv[i][0]=='-' && argc>1;++i) {
  49. if (argv[i][1]=='u'){ user=&argv[i][2];
  50. } else if (argv[i][1]=='p'){ pass=&argv[i][2];
  51. } else if (argv[i][1]=='d'){ database=&argv[i][2];
  52. } else if (argv[i][1]=='s'){ server=&argv[i][2];
  53. } else if (argv[i][1]=='o'){ std_err=stdout;
  54. } else if (argv[i][1]=='a'){ print_args=1;
  55. } else if (argv[i][1]=='q'){ query=&argv[i][2];
  56. } else if (argv[i][1]=='h'){ print_help=1;
  57. } else if (argv[i][1]=='j'){ print_json=1;
  58. } else {//unkown arguement
  59. print_help=1;
  60. }
  61. }
  62. if(print_help || argc==1) {
  63. printf("%s\n",help_message);
  64. exit(0);
  65. }
  66. if(print_args) {
  67. fprintf(std_err,"______________\nRequired Values\nUser:%s Pass:%s Server:%s Database:%s\nQuery:%s\n______________\n\n",user,pass,database,server,query);
  68. }
  69.  
  70.  
  71. // mysql_connectionect to database
  72. if (!mysql_real_connect(mysql_connection, server, user,pass, database, 0, NULL, 0)) {
  73. fprintf(std_err,"Error mysql_connectionecting to database:%s\n", mysql_error(mysql_connection));
  74. exit(1);
  75. }
  76. // send query
  77. if (mysql_query(mysql_connection,query )) {
  78. fprintf(std_err, "%s\n", mysql_error(mysql_connection));
  79. exit(1);
  80. }
  81.  
  82. mysql_result = mysql_store_result(mysql_connection);
  83.  
  84. if(mysql_result){
  85. if(print_json) {
  86. print_result_json(mysql_result);
  87. } else {
  88. print_result_csv(mysql_result);
  89. }
  90. } else {
  91. fprintf(std_err,"Error retrieving results: %s",mysql_error(mysql_connection));
  92. exit(-1);
  93. }
  94.  
  95. mysql_free_result(mysql_result);
  96. mysql_close(mysql_connection);
  97. exit(0);
  98. }
  99. void print_result_csv(MYSQL_RES *result) {
  100. int j,cols = mysql_num_fields(result);
  101. MYSQL_ROW row;
  102. while((row = mysql_fetch_row(result)) != NULL) {
  103. for(j=0;j<cols;++j) {
  104. printf("%s", row[j]);
  105. if(j+1<cols) {
  106. printf(",");
  107. }
  108. }
  109. }
  110. }
  111. void print_result_json(MYSQL_RES *result) {
  112. MYSQL_ROW row;
  113. char *json=malloc(sizeof(*json) * 512);
  114. memset(json,'\0',sizeof(json));
  115. my_ulonglong rows = mysql_num_rows(result);
  116. unsigned int cols = mysql_num_fields(result);
  117. my_ulonglong r=0;
  118. unsigned int c=0;
  119. char row_name[15];
  120. strcpy(&json[strlen(json)],"[");
  121. for(r;r<rows;++r) {
  122. if(row=mysql_fetch_row(result)) {//row is not null
  123. strcpy(&json[strlen(json)],"{");
  124. for(c;c<cols;++c) {
  125. sprintf(row_name,"%u",c);
  126. strcpy(&json[strlen(json)],"\"");
  127. strcpy(&json[strlen(json)],row_name);
  128. strcpy(&json[strlen(json)],"\":\"");
  129. strcpy(&json[strlen(json)],row[c]);
  130. strcpy(&json[strlen(json)],"\"");
  131. if(c+1<cols)
  132. strcpy(&json[strlen(json)],",");
  133. }
  134. strcpy(&json[strlen(json)],"}");
  135. if(r+1<rows)
  136. strcpy(&json[strlen(json)],",");
  137. }
  138. strcpy(&json[strlen(json)],"]");
  139. }
  140. printf("%s",json);
  141.  
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement