Http://topic.csdn.net/t/20031217/13/2573130.html
/*
Name: mysqlclienttest
Author: Kip Warner (kip@zero47.com)
Date: 24/11/03
Description: example to show usage of MySQL databases from client end.
I did not have much time. Sorry...
*/
// Includes...
# Include <windows. h>
# Include <MySQL/MySQL. h>
# Include <stdlib. h>
# Include <stdio. h>
# Include <conio. h>
// Database name...
Char g_szhost [] = "localhost ";
Uint g_unport = mysql_port;
Char g_szusername [] = "charlieface ";
Char g_szpassword [] = "Pudgy ";
Char g_szdatabase [] = "Candy ";
Char g_szsqlstatement [] = "select * chocolates ";
// Entry point...
Int main (INT narguments, char * pszarguments [])
{
// Variables...
MySQL * mydatabase = NULL;
Mysql_res * myresult = NULL;
Mysql_field * myfield = NULL;
Mysql_row myrow = NULL;
Uint unrecords = 0;
Uint unfields = 0;
Uint unindex = 0;
Uint unfieldindex = 0;
// Initialize MySQL...
Mydatabase = mysql_init (null );
// Failed...
If (! Mydatabase)
{
// Alert user...
Printf ("] error: Unable to initialize MySQL API.../N ");
// Cleanup, abort, terminate...
Mysql_close (mydatabase );
Getch ();
Return 0;
}
// Connect to server and check for error...
If (mysql_real_connect (mydatabase, g_szhost, g_szusername, g_szpassword,
Null, g_unport, null, 0 )! = 0)
{
// Alert user...
Printf ("] error: Unable to connect to server.../N ");
// Cleanup, abort, terminate...
Mysql_close (mydatabase );
Getch ();
Return 0;
}
// Select database in server and check for error...
If (mysql_select_db (mydatabase, g_szdatabase) <0)
{
// Alert user...
Printf ("] error: unable to select database.../N ");
// Cleanup, abort, terminate...
Mysql_close (mydatabase );
Getch ();
Return 0;
}
// Query database and check for error...
If (mysql_query (mydatabase, g_szsqlstatement )! = 0)
{
// Alert user...
Printf ("] error: unable to execute query.../N ");
// Cleanup, abort, terminate...
Mysql_close (mydatabase );
Getch ();
Return 0;
}
// Retrieve query result from server...
Myresult = mysql_store_result (mydatabase );
// Failed...
If (! Myresult)
{
// Alert user...
Printf ("] error: unable to retrieve result.../N ");
// Cleanup, abort, terminate...
Mysql_close (mydatabase );
Getch ();
Return 0;
}
// How many records were returned in the result set?
// Calculate...
Unrecords = mysql_num_rows (myresult );
// Alert user...
Printf ("] query: % d records found.../N", unrecords );
// How many fields are present in a record?
// Calculate...
Unfields = mysql_num_fields (myresult );
// Alert user...
Printf ("] query: there are % d fields in each record...", unfields );
// Output records...
For (unindex = 0; unindex <unrecords; unindex ++)
{
// Fetch row from results...
Myrow = mysql_fetch_row (myresult );
// Fetch fields from row...
Myfield = mysql_fetch_fields (myresult );
// Show record...
Printf ("] record: % d/N", unindex, unrecords );
// Output all fields in this row...
For (unfieldindex = 0; unfieldindex <unfields; unfieldindex ++)
{
// Output...
Printf ("/T % s", myfield [unfieldindex]. Name );
}
}
// Free result...
Mysql_free_result (myresult );
// Close server connection...
Mysql_close (mydatabase );
Mydatabase = NULL;
// Alert user, exit...
Printf ("] Done, press any key to exit.../N ");
Getch ();
Return 0;
}