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--- -
#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(myData->net),offsetof(MYSQL,net));
-
printf("host:\t%3d %3d\n",sizeof(myData->host),offsetof(MYSQL,host));
-
printf("port:\t%3d %3d\n",sizeof(myData->port),offsetof(MYSQL,port));
-
printf("protocol_version:\t%3d %3d\n",sizeof(myData->protocol_version),
-
offsetof(MYSQL,protocol_version));
-
printf("thread_id:\t%3d %3d\n",sizeof(myData->thread_id),
-
offsetof(MYSQL,thread_id));
-
printf("affected_rows:\t%3d %3d\n",sizeof(myData->affected_rows),
-
offsetof(MYSQL,affected_rows));
-
printf("packet_length:\t%3d %3d\n",sizeof(myData->packet_length),
-
offsetof(MYSQL,packet_length));
-
printf("status:\t%3d %3d\n",sizeof(myData->status),
-
offsetof(MYSQL,status));
-
printf("fields:\t%3d %3d\n",sizeof(myData->fields),
-
offsetof(MYSQL,fields));
-
printf("field_alloc:\t%3d %3d\n",sizeof(myData->field_alloc),
-
offsetof(MYSQL,field_alloc));
-
printf("free_me:\t%3d %3d\n",sizeof(myData->free_me),
-
offsetof(MYSQL,free_me));
-
printf("options:\t%3d %3d\n",sizeof(myData->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((MYSQL*) 0)) &&
-
mysql_real_connect( 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_result( 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_field( res ) ; x++ )
-
strcpy( aszFlds[ x ], fd->name ) ;
-
//....
-
while ( row = mysql_fetch_row( res ) ) {
-
j = mysql_num_fields( 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)||(!strlen(row[k])))?"NULL":row[k])) ;
-
puts( "==============================\n" ) ;
-
}
-
mysql_free_result( res ) ;
-
}
-
else printf( "Couldn't execute %s on the server !\n", szSQL ) ;
-
//....
-
puts( "==== Diagnostic info ====" ) ;
-
pszT = mysql_get_client_info() ;
-
printf( "Client info: %s\n", pszT ) ;
-
//....
-
pszT = mysql_get_host_info( myData ) ;
-
printf( "Host info: %s\n", pszT ) ;
-
//....
-
pszT = mysql_get_server_info( myData ) ;
-
printf( "Server info: %s\n", pszT ) ;
-
//....
-
res = mysql_list_processes( myData ) ; l = 1 ;
-
if (res)
-
{
-
for ( x = 0 ; fd = mysql_fetch_field( res ) ; x++ )
-
strcpy( aszFlds[ x ], fd->name ) ;
-
while ( row = mysql_fetch_row( res ) ) {
-
j = mysql_num_fields( 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)||(!strlen(row[k])))?"NULL":row[k])) ;
-
puts( "==============================\n" ) ;
-
}
-
}
-
else
-
{
-
printf("Got error %s when retreiving processlist\n",mysql_error(myData));
-
}
-
//....
-
res = mysql_list_tables( myData, "%" ) ; l = 1 ;
-
for ( x = 0 ; fd = mysql_fetch_field( res ) ; x++ )
-
strcpy( aszFlds[ x ], fd->name ) ;
-
while ( row = mysql_fetch_row( res ) ) { j = mysql_num_fields( 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)||(!strlen(row[k])))?"NULL":row[k])) ;
-
puts( "==============================\n" ) ;
-
// int sol;
-
// sol= mysql> SELECT absTestID, Messwert, TestID FROM abstest;
-
// printf("Resultant value is %d\n",sol);
-
}
-
//....
-
pszT = mysql_stat( myData ) ;
-
puts( pszT ) ;
-
//....
-
mysql_close( myData ) ;
-
return 0 ;
-
-
}
Kindly help me in this case as soon as possible
Thanks and Regards in advance
Dhanekula. Siva
22 3208 donbock 2,426
Recognized Expert Top Contributor
Do you have a specific question?
... Are you getting a build error?
... Is the program malfunctioning?
Thanks for the reply
The given program is running and even I am getting output for that. but my question is how to extract the data from the above program and the output should be in CSV format.
i.e. If I run the above program the output is the list of processes and tables from mydatabase and I need to extract required data from any of the tables so that if I press the Run button I should directly get CSV output....This is what I need
Looks like you are using tabs to separate your data? You'd need to change those tabs into commas. Outputting to a file is easy. You can then easily redirect your printf's to print to a file instead using something like -
FILE *fp;
-
if((fp=freopen("file.csv", "w" ,stdout))==NULL) {
-
printf("Cannot open file.\n");
-
exit(1);
-
}
-
printf("All printfs should now be printing to the file.");
-
-
//don't forget somewhere at the end to close the fp
-
fclose(fp);
EDIT: On second thought, why are you doing all this when MySQL has a handy
command
Thank you for the reply
I think you didn't understand my ques may be bcoz of my poor english.
My problem is:: I was given a database with lot of tables in it and my work is to extract some data from any one of the table and I have to analyse the data through MINITAB 15(out of question). I can do it directly in MYSQL front end but, my TL asked me to try it in othér way. i.e. he wants a C/C++ code that extracts the data and get the out put as CSV format. For that I have taken the example program that was given in mysql and changed the database and when I am running it I am just getting the list of the tables. Now my question is how to extract a certain table from that and how to extract the data from that table and how can I get it in .CSV format
Thanks in advance
Siva
You need to use mysql_query (or mysql_real_quer y) for the select statement from the tables themselves then use mysql_use_resul ts to process the results. You can about all that from the refmanual itself.
P.S Your English is fine.
Thank you
By using the mentioned commands I am able to extract the data from the table but I am not getting the output in CSV format. Can you provide me any code for doing so in C-lan(I think by using file"fprintf")
Regards
Dhanekula.Siva
Now read my reply #4 above again.
Hi
ya I have tried with that but I got the following error message
C: \ Program Files \ Microsoft Visual Studio \ MyProject \ winutms \ winutms.cpp (180): error C2664: 'freopen': conversion of the parameter 3 of 'char *' in 'struct _iobuf *' not possible
Can you tell me What is "w" in the given program?
and What is the difference between fopen and freopen?
Thankyou
Banfa 9,065
Recognized Expert Moderator Expert
Did you put this
if((fp=freopen( "file.csv", "w" ,"stdout"))==NU LL) {
instead of this
if((fp=freopen( "file.csv", "w" ,stdout))==NULL ) {
as post 4 specified?
The "w" instructs the function to create a handle to a writeable file, i.e. an output file not an input file.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Trader |
last post by:
Hi,
I'm trying to use Mark Hammond's win32clipboard module to extract more
complex data than just plain ASCII text from the Windows clipboard.
For instance, when you select all the content on web page, you can
paste it into an app like Frontpage, or something Rich Text-aware, and
it will preserve all the formatting, HTML, etc. I'd like to...
|
by: Henok Girma |
last post by:
Hello Gurus,
I wanted to have a very simple, strict regular expression validator for a
phone field. the only valid format is
(XXX) XXX-XXXX where X is a digit
I have the following but it always evaluates to false
var reg = new RegExp("^\(\d{3}\) \d{3}-\d{4}$");
alert(reg.test("(123) 221-9244"));
Any help is greatly appreciated..
|
by: worzel |
last post by:
Hi All,
I am looking for a reg ex that will match email addresses withing <a
href=mailto blah > links.
Actually, I already crafted my own, but with a slight problem:
<a href="mailto:fred@blah.com"> emal me</a> woud be matched as expected, but
so will:
|
by: dave |
last post by:
Hi there,
I'm trying to extract formatting information from the columns in a
database field and use it to format data bound text boxes in a VB6
application. I can't find a way to get the format information from the
ADO database fields. Can anyone help me at all, I'm really stuck and
pretty desparate! An example, or a link to an example is...
|
by: sgsiaokia |
last post by:
I need help in extracting data from another source file using VBA. I have problems copying the extracted data and format into the required data format. And also, how do i delete the row that is not required in the output file, in the below example: The row, D0, is not needed.
An Example Data Format From the SOURCE file:
...
| |
by: Werner |
last post by:
Hi,
I try to read (and extract) some "self extracting" zipefiles on a
Windows system. The standard module zipefile seems not to be able to
handle this.
False
Is there a wrapper or has some one experience with other libaries to
|
by: LeWalrus |
last post by:
Hi, I've written a reg exp for capturing a group of numbers from text files in the following format:
-1.4326 s < 0.6758 s < 1.4334 s
Any of the numbers can be positive or negative and the units (s) can change or even be absent. What I wanted was the three numbers (signs included)! Here was the reg exp I used to capture:
$line =~...
|
by: sivadhanekula |
last post by:
Hi Everyone
A quick question: How to write MYSQL command in C-Language
MYSQL Statement to select particular row from a table is:
SELECT User FROM absprfg
where User is the heading of the row
absprfg is the table name
|
by: sivadhanekula |
last post by:
Hi everyone,
I have a problem with my Mysql data. I have 2,95,67,456 lines of data which is too much and if I run this in MYSQL front-end it is telling "OUT OF MEMORY". Any way with my collegues system I have got the runned data and I have copied it to my Frontend, now it works. But my problem is if I am extracting particular data from that it...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |