Hello Everyone
A quick and direct question: I need a C/C++ program to extract the data from the database and the output should be in CSV "Comma Separated Value" format.
Briefly: I will be given a database and my work is to extract a particular data from that whole database and should get a output in CSV format. The database given to me has 24 tables and I need to extract data from any one of the table and if I press the run button I should get the output directly in CSV format.
Program what I have: The program what I have is just to get the details of each table---[CODE=c]
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <mysql.h>
#define DEFALT_SQL_STMT "SELECT * FROM db"
#ifndef offsetof
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#endif
int
main( int argc, char * argv[] )
{
char szSQL[ 200 ], aszFlds[ 25 ][ 25 ], szDB[ 50 ] ;
const char *pszT;
int i, j, k, l, x ;
MYSQL * myData ;
MYSQL_RES * res ;
MYSQL_FIELD * fd ;
MYSQL_ROW row ;
//....just curious....
printf( "sizeof( MYSQL ) == %d\n", sizeof( MYSQL) ) ;
if ( argc == 2 )
{
strcpy( szDB, argv[ 1 ] ) ;
strcpy( szSQL, DEFALT_SQL_STMT ) ;
if (!strcmp(szDB,"--debug"))
{
strcpy( szDB, "mysql" ) ;
printf("Some mysql struct information (size and offset):\n");
printf("net:\t% 3d %3d\n",sizeof(m yData->net),offsetof( MYSQL,net));
printf("host:\t %3d %3d\n",sizeof(m yData->host),offsetof (MYSQL,host));
printf("port:\t %3d %3d\n",sizeof(m yData->port),offsetof (MYSQL,port));
printf("protoco l_version:\t%3d %3d\n",sizeof(m yData->protocol_versi on),
offsetof(MYSQL, protocol_versio n));
printf("thread_ id:\t%3d %3d\n",sizeof(m yData->thread_id),
offsetof(MYSQL, thread_id));
printf("affecte d_rows:\t%3d %3d\n",sizeof(m yData->affected_rows) ,
offsetof(MYSQL, affected_rows)) ;
printf("packet_ length:\t%3d %3d\n",sizeof(m yData->packet_length) ,
offsetof(MYSQL, packet_length)) ;
printf("status: \t%3d %3d\n",sizeof(m yData->status),
offsetof(MYSQL, status));
printf("fields: \t%3d %3d\n",sizeof(m yData->fields),
offsetof(MYSQL, fields));
printf("field_a lloc:\t%3d %3d\n",sizeof(m yData->field_alloc) ,
offsetof(MYSQL, field_alloc));
printf("free_me :\t%3d %3d\n",sizeof(m yData->free_me),
offsetof(MYSQL, free_me));
printf("options :\t%3d %3d\n",sizeof(m yData->options),
offsetof(MYSQL, options));
puts("");
}
}
else if ( argc > 2 ) {
strcpy( szDB, argv[ 1 ] ) ;
strcpy( szSQL, argv[ 2 ] ) ;
}
else {
strcpy( szDB, "winutms_rt _db" ) ;
strcpy( szSQL, DEFALT_SQL_STMT ) ;
}
//....
if ( (myData = mysql_init((MYS QL*) 0)) &&
mysql_real_conn ect( myData, NULL, NULL, NULL, NULL, MYSQL_PORT,
NULL, 0 ) )
{
if ( mysql_select_db ( myData, szDB ) < 0 ) {
printf( "Can't select the %s database !\n", szDB ) ;
mysql_close( myData ) ;
return 2 ;
}
}
else {
printf( "Can't connect to the mysql server on port %d !\n",
MYSQL_PORT ) ;
mysql_close( myData ) ;
return 1 ;
}
//....
if ( ! mysql_query( myData, szSQL ) ) {
res = mysql_store_res ult( myData ) ;
i = (int) mysql_num_rows( res ) ; l = 1 ;
printf( "Query: %s\nNumber of records found: %ld\n", szSQL, i ) ;
//....we can get the field-specific characteristics here....
for ( x = 0 ; fd = mysql_fetch_fie ld( res ) ; x++ )
strcpy( aszFlds[ x ], fd->name ) ;
//....
while ( row = mysql_fetch_row ( res ) ) {
j = mysql_num_field s( res ) ;
printf( "Record #%ld:-\n", l++ ) ;
for ( k = 0 ; k < j ; k++ )
printf( " Fld #%d (%s): %s\n", k + 1, aszFlds[ k ],
(((row[k]==NULL)||(!strl en(row[k])))?"NULL":row[k])) ;
puts( "============== =============== =\n" ) ;
}
mysql_free_resu lt( res ) ;
}
else printf( "Couldn't execute %s on the server !\n", szSQL ) ;
//....
puts( "==== Diagnostic info ====" ) ;
pszT = mysql_get_clien t_info() ;
printf( "Client info: %s\n", pszT ) ;
//....
pszT = mysql_get_host_ info( myData ) ;
printf( "Host info: %s\n", pszT ) ;
//....
pszT = mysql_get_serve r_info( myData ) ;
printf( "Server info: %s\n", pszT ) ;
//....
res = mysql_list_proc esses( myData ) ; l = 1 ;
if (res)
{
for ( x = 0 ; fd = mysql_fetch_fie ld( res ) ; x++ )
strcpy( aszFlds[ x ], fd->name ) ;
while ( row = mysql_fetch_row ( res ) ) {
j = mysql_num_field s( res ) ;
printf( "Process #%ld:-\n", l++ ) ;
for ( k = 0 ; k < j ; k++ )
printf( " Fld #%d (%s): %s\n", k + 1, aszFlds[ k ],
(((row[k]==NULL)||(!strl en(row[k])))?"NULL":row[k])) ;
puts( "============== =============== =\n" ) ;
}
}
else
{
printf("Got error %s when retreiving processlist\n", mysql_error(myD ata));
}
//....
res = mysql_list_tabl es( myData, "%" ) ; l = 1 ;
for ( x = 0 ; fd = mysql_fetch_fie ld( res ) ; x++ )
strcpy( aszFlds[ x ], fd->name ) ;
while ( row = mysql_fetch_row ( res ) ) { j = mysql_num_field s( res ) ;
printf( "Table #%ld:-\n", l++ ) ;
for ( k = 0 ; k < j ; k++ )
printf( " Fld #%d (%s): %s\n", k + 1, aszFlds[ k ],
(((row[k]==NULL)||(!strl en(row[k])))?"NULL":row[k])) ;
puts( "============== =============== =\n" ) ;
// int sol;
// sol= mysql> SELECT absTestID, Messwert, TestID FROM abstest;
// printf("Resulta nt value is %d\n",sol);
}
//....
pszT = mysql_stat( myData ) ;
puts( pszT ) ;
//....
mysql_close( myData ) ;
return 0 ;
}[/CODE]
Kindly help me in this case as soon as possible
Thanks and Regards in advance
Dhanekula. Siva
A quick and direct question: I need a C/C++ program to extract the data from the database and the output should be in CSV "Comma Separated Value" format.
Briefly: I will be given a database and my work is to extract a particular data from that whole database and should get a output in CSV format. The database given to me has 24 tables and I need to extract data from any one of the table and if I press the run button I should get the output directly in CSV format.
Program what I have: The program what I have is just to get the details of each table---[CODE=c]
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <mysql.h>
#define DEFALT_SQL_STMT "SELECT * FROM db"
#ifndef offsetof
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#endif
int
main( int argc, char * argv[] )
{
char szSQL[ 200 ], aszFlds[ 25 ][ 25 ], szDB[ 50 ] ;
const char *pszT;
int i, j, k, l, x ;
MYSQL * myData ;
MYSQL_RES * res ;
MYSQL_FIELD * fd ;
MYSQL_ROW row ;
//....just curious....
printf( "sizeof( MYSQL ) == %d\n", sizeof( MYSQL) ) ;
if ( argc == 2 )
{
strcpy( szDB, argv[ 1 ] ) ;
strcpy( szSQL, DEFALT_SQL_STMT ) ;
if (!strcmp(szDB,"--debug"))
{
strcpy( szDB, "mysql" ) ;
printf("Some mysql struct information (size and offset):\n");
printf("net:\t% 3d %3d\n",sizeof(m yData->net),offsetof( MYSQL,net));
printf("host:\t %3d %3d\n",sizeof(m yData->host),offsetof (MYSQL,host));
printf("port:\t %3d %3d\n",sizeof(m yData->port),offsetof (MYSQL,port));
printf("protoco l_version:\t%3d %3d\n",sizeof(m yData->protocol_versi on),
offsetof(MYSQL, protocol_versio n));
printf("thread_ id:\t%3d %3d\n",sizeof(m yData->thread_id),
offsetof(MYSQL, thread_id));
printf("affecte d_rows:\t%3d %3d\n",sizeof(m yData->affected_rows) ,
offsetof(MYSQL, affected_rows)) ;
printf("packet_ length:\t%3d %3d\n",sizeof(m yData->packet_length) ,
offsetof(MYSQL, packet_length)) ;
printf("status: \t%3d %3d\n",sizeof(m yData->status),
offsetof(MYSQL, status));
printf("fields: \t%3d %3d\n",sizeof(m yData->fields),
offsetof(MYSQL, fields));
printf("field_a lloc:\t%3d %3d\n",sizeof(m yData->field_alloc) ,
offsetof(MYSQL, field_alloc));
printf("free_me :\t%3d %3d\n",sizeof(m yData->free_me),
offsetof(MYSQL, free_me));
printf("options :\t%3d %3d\n",sizeof(m yData->options),
offsetof(MYSQL, options));
puts("");
}
}
else if ( argc > 2 ) {
strcpy( szDB, argv[ 1 ] ) ;
strcpy( szSQL, argv[ 2 ] ) ;
}
else {
strcpy( szDB, "winutms_rt _db" ) ;
strcpy( szSQL, DEFALT_SQL_STMT ) ;
}
//....
if ( (myData = mysql_init((MYS QL*) 0)) &&
mysql_real_conn ect( myData, NULL, NULL, NULL, NULL, MYSQL_PORT,
NULL, 0 ) )
{
if ( mysql_select_db ( myData, szDB ) < 0 ) {
printf( "Can't select the %s database !\n", szDB ) ;
mysql_close( myData ) ;
return 2 ;
}
}
else {
printf( "Can't connect to the mysql server on port %d !\n",
MYSQL_PORT ) ;
mysql_close( myData ) ;
return 1 ;
}
//....
if ( ! mysql_query( myData, szSQL ) ) {
res = mysql_store_res ult( myData ) ;
i = (int) mysql_num_rows( res ) ; l = 1 ;
printf( "Query: %s\nNumber of records found: %ld\n", szSQL, i ) ;
//....we can get the field-specific characteristics here....
for ( x = 0 ; fd = mysql_fetch_fie ld( res ) ; x++ )
strcpy( aszFlds[ x ], fd->name ) ;
//....
while ( row = mysql_fetch_row ( res ) ) {
j = mysql_num_field s( res ) ;
printf( "Record #%ld:-\n", l++ ) ;
for ( k = 0 ; k < j ; k++ )
printf( " Fld #%d (%s): %s\n", k + 1, aszFlds[ k ],
(((row[k]==NULL)||(!strl en(row[k])))?"NULL":row[k])) ;
puts( "============== =============== =\n" ) ;
}
mysql_free_resu lt( res ) ;
}
else printf( "Couldn't execute %s on the server !\n", szSQL ) ;
//....
puts( "==== Diagnostic info ====" ) ;
pszT = mysql_get_clien t_info() ;
printf( "Client info: %s\n", pszT ) ;
//....
pszT = mysql_get_host_ info( myData ) ;
printf( "Host info: %s\n", pszT ) ;
//....
pszT = mysql_get_serve r_info( myData ) ;
printf( "Server info: %s\n", pszT ) ;
//....
res = mysql_list_proc esses( myData ) ; l = 1 ;
if (res)
{
for ( x = 0 ; fd = mysql_fetch_fie ld( res ) ; x++ )
strcpy( aszFlds[ x ], fd->name ) ;
while ( row = mysql_fetch_row ( res ) ) {
j = mysql_num_field s( res ) ;
printf( "Process #%ld:-\n", l++ ) ;
for ( k = 0 ; k < j ; k++ )
printf( " Fld #%d (%s): %s\n", k + 1, aszFlds[ k ],
(((row[k]==NULL)||(!strl en(row[k])))?"NULL":row[k])) ;
puts( "============== =============== =\n" ) ;
}
}
else
{
printf("Got error %s when retreiving processlist\n", mysql_error(myD ata));
}
//....
res = mysql_list_tabl es( myData, "%" ) ; l = 1 ;
for ( x = 0 ; fd = mysql_fetch_fie ld( res ) ; x++ )
strcpy( aszFlds[ x ], fd->name ) ;
while ( row = mysql_fetch_row ( res ) ) { j = mysql_num_field s( res ) ;
printf( "Table #%ld:-\n", l++ ) ;
for ( k = 0 ; k < j ; k++ )
printf( " Fld #%d (%s): %s\n", k + 1, aszFlds[ k ],
(((row[k]==NULL)||(!strl en(row[k])))?"NULL":row[k])) ;
puts( "============== =============== =\n" ) ;
// int sol;
// sol= mysql> SELECT absTestID, Messwert, TestID FROM abstest;
// printf("Resulta nt value is %d\n",sol);
}
//....
pszT = mysql_stat( myData ) ;
puts( pszT ) ;
//....
mysql_close( myData ) ;
return 0 ;
}[/CODE]
Kindly help me in this case as soon as possible
Thanks and Regards in advance
Dhanekula. Siva
Comment