Friday, May 27, 2016

An example of Zenity and the MySql query in C++

/*
 * ALfred Haines Copyright 2013
 *
 * This example of Zenity and the MySql query
 * Both must be installed
 *
 *
 * g++  cpp/main.cpp -Wno-deprecated -Wno-write-strings  -DQT_SQL_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/include/mysql -I/usr/include/mysql++ -L/usr/lib  -L/usr/lib -lpthread  -I../ cpp/dr01.cpp -lmysqlpp -lmysqlclient -lnsl -lz -lm -o dr01
 *
 * you must setup a connection file for this to access MySql
 * I call mine con.cpp and it is in my cpp folder
 * it will define
 * SERVER="localhost",USER="root",PASSWORD="",DATABASE="DBase"
 *
 *
 *
*
#include "sstream>"
#include "boost/tokenizer.hpp>"
#include "boost/iostreams/device/file.hpp>"
#include "boost/iostreams/stream.hpp>"
#include "boost/algorithm/string/replace.hpp>"
#include "vector>"
#include "mysql.h>"
#include "con.cpp" // MySQL password File
#include "stdio.h>"
#include "iostream>"
#include "cstdlib>"
#include "string>"

using namespace std;
MYSQL *connection, mysql;
MYSQL_RES *result;
MYSQL_ROW row;
int query_state;

#define N_MOVIES 300
#define N_ReGen 12

struct drs_t {
  string name ;
  int id ;
} actor [N_ReGen];

struct movies_t {
  string title;
  int year;
} films [N_MOVIES];

void printmovie (movies_t movie);
std::string exec(char* cmd);

int main() {
    string rs ; string re ;
    mysql_init(&mysql);
//connection = mysql_real_connect(&mysql,"host","user","password","database",0,0,0);
connection = mysql_real_connect(&mysql,SERVER ,USER ,PASSWORD ,DATABASE ,0,0,0);
if (connection == NULL) {
cout << mysql_error(&mysql) << endl;
return 1;
}
string tm ; int c = 0 ;
query_state = mysql_query(connection, "select * FROM drs ");
if (query_state !=0) {
cout << mysql_error(connection) << endl;
return 1;
}
string cm = "ans=$(zenity  --list  --height 400 --width 350  --text \"Choose a Doctor\" --radiolist --column \"Pick\" --column \"Num\" --column \"Name\"";
result = mysql_store_result(connection);
while ( ( row = mysql_fetch_row(result)) != NULL ) {

tm = " ";
tm.append( "FALSE \"" );
tm.append( row[0] );
tm.append( "\" \"");
tm.append( row[1] );
tm.append( "\" ");
cm.append( tm );
}

cm.append( " ); echo $ans" );
cm.erase(remove( cm.begin(), cm.end(), '`' ),cm.end());
char* cmd = strdup(cm.c_str());
re = exec( cmd);

// string j = "select * FROM drwho WHERE intid = 1";
// string j = "select  drs.name, drwho.*  FROM drwho, drs   WHERE drwho.intid = 1";
//sprintf(qu,"%s" , "select  drs.name, drwho.*  FROM drwho, drs   WHERE drs.id = " );

string j = "select * FROM drwho WHERE dr = ";
j.append(re);

query_state = mysql_query(connection, j.c_str());
if (query_state !=0) {
cout << mysql_error(connection) << endl;
return 1;
}
cm = "ans=$(zenity  --list  --height 400 --width 350  --text \"Choose an Episode \" --radiolist --column \"Pick\" --column \"Num\" --column \"Name\"";
result = mysql_store_result(connection);
while ( ( row = mysql_fetch_row(result)) != NULL ) {
// cout << row[6] << endl;
tm = " ";
tm.append( "FALSE \"" );
tm.append( row[0] );
tm.append( "\" \"");
tm.append( row[4] );
tm.append( "\" ");
cm.append( tm );
}
cm.append( " ); echo $ans" );
cm.erase(remove( cm.begin(), cm.end(), '`' ),cm.end());
cmd = strdup(cm.c_str());
re = exec( cmd);
j = "select * FROM drwho WHERE intid = ";
j.append(re);
query_state = mysql_query(connection, j.c_str() );
if (query_state !=0) {
cout << mysql_error(connection) << endl;
return 1;
}
cm = "echo ";
result = mysql_store_result(connection);
while ( ( row = mysql_fetch_row(result)) != NULL ) {
// cout << row[6] << endl;
tm = " ";
tm.append( "\"");
tm.append( row[5] );
tm.append( "\" ");
cm.append( tm );
}
cm.append( "  | zenity --text-info --height 300 --width 650 " );
cm.erase(remove( cm.begin(), cm.end(), '`' ),cm.end());
cmd = strdup(cm.c_str());
re = exec( cmd);
mysql_free_result(result);
mysql_close(connection);
return 0;
}

std::string exec(char* cmd)
  {
    FILE* pipe = popen(cmd, "r");
    if (!pipe) return "ERROR";
    char buffer[128];
    std::string result = "";
    while(!feof(pipe)) {
    if(fgets(buffer, 128, pipe) != NULL)
    result += buffer;
    }
    pclose(pipe);
    return result;
}
void printmovie (movies_t movie)
{
  cout << movie.title;
  cout << " (" << movie.year << ")\n";
}

No comments:

Post a Comment