Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- -SheldonJ22, software provided as is. Feel free to use, modify and redistribute without restriction.
- mysql.exe Version 1.0
- Tested on Win10 and Win Srv 2012R2 with MariaDB
- */
- #include <stdio.h>
- #include "include/mysql.h"
- void print_result_csv(MYSQL_RES *result);
- void print_result_json(MYSQL_RES *result);
- int main(int argc, char *argv[]) {
- MYSQL *mysql_connection;
- MYSQL_RES *mysql_result;
- mysql_connection = mysql_init(NULL);
- char *user;
- char *pass;
- char *database;
- char *server;
- char *query;
- int i=1;
- char print_args=0;//!0 prints user arguements
- char print_help=0;//!0 prints help
- char print_json=0;//!0 prints json
- FILE *std_err = stderr;//output stream - can be set to stdout by user
- char *help_message="\nCommand line usage\n \
- -a [optional] print the argumentst that you entered\n \
- -d [required] the database you will mysql_connectionect to\n \
- -h [optional] print this help file\n \
- -j [optional] print output in json format\n \
- -o [optional] redirect stderr to stdout\n \
- -p [required] password for user to mysql_connectionect to database\n \
- -q [required] the query to send\n \
- -s [required] server to mysql_connectionect to , IP or hostname\n \
- -u [required] username for database\n \
- \n \
- Example usage:\n \
- mysql.exe -uusername -ppassword -ddatabasename -s\"1.2.3.4\" -q\"select * from database.table where field='value'\"\n \
- Output:\n \
- 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 \
- Json format: [{\"0\":\"value\",\"1\",\"value\"},{\"0\":\"value\",\"1\",\"value\"}]\n \
- Troubleshooting\n \
- Be sure to have libmysql.dll in same directory as execution\n \
- Be sure you have given proper access on the DB server\n \
- Be sure you have opened up necessary ports on DB server\n \
- \n\n";
- for(i;i<argc && argv[i][0]=='-' && argc>1;++i) {
- if (argv[i][1]=='u'){ user=&argv[i][2];
- } else if (argv[i][1]=='p'){ pass=&argv[i][2];
- } else if (argv[i][1]=='d'){ database=&argv[i][2];
- } else if (argv[i][1]=='s'){ server=&argv[i][2];
- } else if (argv[i][1]=='o'){ std_err=stdout;
- } else if (argv[i][1]=='a'){ print_args=1;
- } else if (argv[i][1]=='q'){ query=&argv[i][2];
- } else if (argv[i][1]=='h'){ print_help=1;
- } else if (argv[i][1]=='j'){ print_json=1;
- } else {//unkown arguement
- print_help=1;
- }
- }
- if(print_help || argc==1) {
- printf("%s\n",help_message);
- exit(0);
- }
- if(print_args) {
- fprintf(std_err,"______________\nRequired Values\nUser:%s Pass:%s Server:%s Database:%s\nQuery:%s\n______________\n\n",user,pass,database,server,query);
- }
- // mysql_connectionect to database
- if (!mysql_real_connect(mysql_connection, server, user,pass, database, 0, NULL, 0)) {
- fprintf(std_err,"Error mysql_connectionecting to database:%s\n", mysql_error(mysql_connection));
- exit(1);
- }
- // send query
- if (mysql_query(mysql_connection,query )) {
- fprintf(std_err, "%s\n", mysql_error(mysql_connection));
- exit(1);
- }
- mysql_result = mysql_store_result(mysql_connection);
- if(mysql_result){
- if(print_json) {
- print_result_json(mysql_result);
- } else {
- print_result_csv(mysql_result);
- }
- } else {
- fprintf(std_err,"Error retrieving results: %s",mysql_error(mysql_connection));
- exit(-1);
- }
- mysql_free_result(mysql_result);
- mysql_close(mysql_connection);
- exit(0);
- }
- void print_result_csv(MYSQL_RES *result) {
- int j,cols = mysql_num_fields(result);
- MYSQL_ROW row;
- while((row = mysql_fetch_row(result)) != NULL) {
- for(j=0;j<cols;++j) {
- printf("%s", row[j]);
- if(j+1<cols) {
- printf(",");
- }
- }
- }
- }
- void print_result_json(MYSQL_RES *result) {
- MYSQL_ROW row;
- char *json=malloc(sizeof(*json) * 512);
- memset(json,'\0',sizeof(json));
- my_ulonglong rows = mysql_num_rows(result);
- unsigned int cols = mysql_num_fields(result);
- my_ulonglong r=0;
- unsigned int c=0;
- char row_name[15];
- strcpy(&json[strlen(json)],"[");
- for(r;r<rows;++r) {
- if(row=mysql_fetch_row(result)) {//row is not null
- strcpy(&json[strlen(json)],"{");
- for(c;c<cols;++c) {
- sprintf(row_name,"%u",c);
- strcpy(&json[strlen(json)],"\"");
- strcpy(&json[strlen(json)],row_name);
- strcpy(&json[strlen(json)],"\":\"");
- strcpy(&json[strlen(json)],row[c]);
- strcpy(&json[strlen(json)],"\"");
- if(c+1<cols)
- strcpy(&json[strlen(json)],",");
- }
- strcpy(&json[strlen(json)],"}");
- if(r+1<rows)
- strcpy(&json[strlen(json)],",");
- }
- strcpy(&json[strlen(json)],"]");
- }
- printf("%s",json);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement