Hi All,
Im currently try to write a code to read the data from mysql tables and then write the contents into a text file. Below is my code. I dunt know what's wrong with this code because in the text file created, there is no data from database written. Hope u guys can help me..
Thanks in advance..:)
#include <stdio.h>
#include <string.h>
#include <mysql.h>
#define HOST "localhost"
#define USER "yati"
#define PASSWD "yati"
#define DB_NAME "subscriberTool"
void openFile();
void readFromDB();
static MYSQL demo_db;
//char hp[50], startdate[50], enddate[50];
int main()
{
openFile();
}
void openFile()
{
char line[1600];
char lineone[1600];
char hp[50], startdate[50], enddate[50];
FILE *fp;
fp=fopen("TH.txt", "w");
if(fp==NULL)
{
printf("file cannot be opened!\n");
exit(0);
}
else {
while(!feof(fp))
{
bzero(line, sizeof(line));
fgets(line,1600,fp);
readFromDB();
sprintf(lineone, "%s,%s,%s", hp, startdate, enddate);
printf("Error");
}
}
fclose(fp);
return;
}
void readFromDB()
{
char query[16384];
char query1[16384];
if(!mysql_connect(&demo_db, HOST, USER, PASSWD))
{
printf(mysql_error(&demo_db));
exit(1);
}
if(mysql_select_db(&demo_db, DB_NAME))
{
/* Select the database we want to use */
printf(mysql_error(&demo_db));
exit(1);
}
bzero(query, sizeof(query1));
sprintf(query, "SELECT MobileNumber,StartDate,EndDate FROM Subscriptions order by StartDate asc");
mysql_query(&demo_db, query1);
printf("%s\n",query);
if(mysql_real_query(&demo_db, query, strlen(query)+255))
{
printf(mysql_error(&demo_db));
bzero(query, sizeof(query));
exit(1);
}
mysql_close(&demo_db);
return;
}