|
|
|
2.
An example application
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <msql.h>
#include "fatal.h"
#define MAX_QUERY_LEN 4096
int main( int argc, char *argv[] )
{
int msqlSocket, msqlFieldsCounter, index;
m_result *msqlResultHandle;
m_row msqlResultRow;
char queryString[MAX_QUERY_LEN];
/* connect to database, NULL argument indicates localhost */
if( ( msqlSocket = msqlConnect( NULL ) ) == -1 )
fatal( "%s\n", msqlErrMsg );
if( msqlSelectDB( msqlSocket, "tasks" ) == -1 )
fatal( "%s\n", msqlErrMsg );
/* build query string */
strcpy( queryString, "select * from task_detail" );
if( msqlQuery( msqlSocket, queryString ) == -1 )
{
fprintf( stderr, "%s\n", msqlErrMsg );
return( -1 );
}
/* save the query to a result handle */
msqlResultHandle = msqlStoreResult();
/* determine how many fields were returned */
msqlFieldsCounter = msqlNumFields( msqlResultHandle );
/* spew the results */
while( ( msqlResultRow = msqlFetchRow( msqlResultHandle ) ) != NULL )
{
for( index = 0; index < msqlFieldsCounter; index++ )
printf( "%s ", msqlResultRow[index] );
printf( "\n" );
}
/* free the result handle */
msqlFreeResult( msqlResultHandle );
/* documentation says this returns an int, msql.h indicates void */
msqlClose( msqlSocket );
return( 0 );
}
fatal.h
#ifndef FATAL_H
#define FATAL_H
int fatal( char *, ... );
#endif /* FATAL_H */
fatal.c
/* Standard Headers */
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
int fatal( char *format, ... )
{
va_list args;
va_start( args, format );
vfprintf( stderr, format, args );
va_end( args );
exit( 1 );
}
|
|
|
|
|
Last Modified: 17 December 1997 St. Louis Unix Users Group - Linux SIG |
|