Skip to content

Instantly share code, notes, and snippets.

@mwidjaja1
Created April 14, 2014 00:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mwidjaja1/10607578 to your computer and use it in GitHub Desktop.
Save mwidjaja1/10607578 to your computer and use it in GitHub Desktop.
MySql & Dislin for GNP vs. Life Expectancy
#include <mysql.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
// #include <dislin.h>
#include "/Users/Matthew/dislin/examples/dislin.h"
/* --- PROJECT 5/6: MYSQL & DISLIN INTEGRATION ---
This project was created by Matthew Widjaja. Nov 2013.
This project will import the World MySQL Database and plot it in Dislin.
--- Algorithm Used & Notes ---
Step 0: Import .h Files
On many computers, dislin.h is not saved to the proper location, but
rather some user's home folder. The absolute path to the .h file is noted
in the present version of this file. One will have to change said path to the
proper relative path or the absolute path of their computer.
Step 1: Connect to MySQL Server
The user account (denoted by Username: user1 & Password: user1)
would probably have to be modified on other computers.
Step 2: Retrieving & Saving MySql Data
This will select the Life Expectancy & GNP values from the MySQL
database, where all values obtained cannot be 'Null'. If so, they are left
behind. Each value is then saved to one of two arrays: xray or y1ray.
Step 3: Dislin
We then plot the two arrays in C-Code as a scatterplot.
--- Variables Used ---
fieldsN int 'n' quantities of MySQL Entries in the Database
rowsN int 'n' quantities of MySQL Rows in the Database
xray float The array with GNP Data for the X-Axis
yray float The array with the Life-Expectancy Data for the Y-Axis
--- */
// Declares a function for create an error message if needed
void finish_with_error(MYSQL *con)
{
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}
int main(int argc, char **argv)
{
/* --- STEP 1: CONNECTS TO MYSQL SERVER --- */
MYSQL *con = mysql_init(NULL);
if (con == NULL)
{
fprintf(stderr, "mysql_init() failed\n");
exit(1);
}
if (mysql_real_connect(con, "localhost", "user1", "user1",
"world", 0, NULL, 0) == NULL)
{
finish_with_error(con);
}
/* --- STEP 2: RETRIEVING & SAVING MYSQL DATA --- */
if (mysql_query(con,
"SELECT lifeExpectancy, GNP FROM Country WHERE lifeExpectancy IS NOT NULL AND GNP IS NOT NULL"))
{
finish_with_error(con);
}
MYSQL_RES *result = mysql_store_result(con); // Stores the Results
if (result == NULL)
{
finish_with_error(con);
}
int fieldsN = mysql_num_fields(result); // The 'n' for # of MySql Entries
int rowsN = mysql_num_rows(result); // The 'n' for # of MySql Rows
float xray[rowsN]; // Array for GNP
float y1ray[rowsN]; // Array for Life Expectancy
int i = 0; // Integer Counter
MYSQL_ROW row;
while ((row = mysql_fetch_row(result)))
{
xray[i] = atof(row[1]);
y1ray[i] = atof(row[0]);
printf("%f\t %f \n",xray[i],y1ray[i]);
i++;
}
mysql_free_result(result); // Frees the Memory allocated to the results
mysql_close(con); // Closes the MySql Connection
/* --- STEP 3: DISLIN --- */
metafl ( "png" ); // Output File Format Type
filmod ( "delete" ); // Should prior files be deleted?
setfil ( "dislin_ex01.png" ); // Set File Name for Output
setpag ( "usal" ); // Set Page Window for Output
scrmod ( "reverse" ); // PNG normally has a black BG. This inverses that.
disini ( ); // Initializes Dislin
pagera ( ); // Prints a border
complx ( ); // Uses the Complex Font
axstyp ("rect"); // Creates a Rectangular Axis
name ( "GNP", "x" ); // Names the X-Axis
name ( "Life Expectation", "y" ); // Names the Y-Axis
labdig ( -1, "xy" ); // Sets # of Decimal Places for Axis
// Sets the Axis as (InitialX, FinalX, InitialX, IncrementX, IniY, FinY, IniY, IncY) pts.
graf ( 0.0, 9000000.0, 0.0, 1500000.0, 0.0, 90.0, 0.0, 10.0 );
title ( ); // Initializes Axis
color ( "blue" ); // Sets Tickmark Color
hsymbl ( 15 ); // Sets Tickmark Size
qplsca ( xray, y1ray, rowsN ); // Plots Scatterpot with rowsN points.
disfin ( ); // Terminates Dislin
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment