Read the tutorial section http://www.mysql.com/documentation/m....html#Tutorial of the mysql website. Learn what commands you need to give to get the information you want.
After that read the API section http://www.mysql.com/documentation/m...Clients.html#C to learn the calls.
A basic program is setup as such:
1) Initalize the database connection
2) Connect to the server
3) Select the database to use
4) Give it commands with mysql_query or mysql_real_query
Below is a little program. It works but could use some minor tweaking to make the output prettier.
Code:
#include <stdio.h>
#include <mysql/mysql.h>
int main (void)
{
MYSQL db_conn;
MYSQL_RES *result;
MYSQL_ROW row;
MYSQL_FIELD *fields;
unsigned int numrows;
unsigned int numfields;
int c;
char user[] = "tester";
char password[] = "tester";
char host[] = "localhost";
char dbname[] = "Pics";
char buffer[BUFSIZ];
mysql_init(&db_conn);
if ( mysql_real_connect (&db_conn, host, user, password, dbname, 0, NULL, 0) == NULL)
{
fprintf(stderr, "Could not connect to mysql server. Error %s\n", mysql_error(&db_conn));
return 0;
}
puts ("Connected");
printf("Client Info: %s\n", mysql_get_client_info() );
sprintf(buffer, "SELECT * FROM %s", "Events");
if ( mysql_query(&db_conn, buffer) )
{
fprintf(stderr, "Could not execute the command\n%s\nrecieved the error \n%s\n", buffer, mysql_error(&db_conn));
}
else
{
result = mysql_store_result(&db_conn);
if ( (numrows = mysql_num_rows(result)) )
{
printf("There are %d rows in this result which are:\n", numrows);
numfields = mysql_num_fields(result);
fields = mysql_fetch_fields(result);
for (c=0; c<numfields; c++)
printf("%s\t", fields[c].name);
putchar('\n');
while ( (row = mysql_fetch_row(result)) )
{
for (c=0; c<numfields; c++)
printf("%s\t", row[c]);
putchar('\n');
}
}
else
puts("Empty Set");
mysql_free_result(result);
}
return 0;
}