954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

read from database and writing the contents into a text file

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;
}
whitemoss
Light Poster
32 posts since Jul 2006
Reputation Points: 16
Solved Threads: 0
 

>>there is no data from database written.

Read you program again. Where does it even attempt to write anything to the text file? Where does your program call readFromDB() to get the row from the table? You program consists of two functions, one function that just opens a file then immediately closes it. And a second function that opens the database and executes a query -- it just ignores the result set. Nowhere does your program put those two functions together. You need to do something like this

OPEN THE TEXT FILE FOR WRITE
OPEN THE DATABASE
QUERY THE DATABASE
CALL mysql_field_count() TO SEE IF THE QUERY RETURNED A RESULT SET
FOR EACH RESULTSET ROW (one of the mysql_fetch??? functions)
   WRITE RESULT ROW TO TEXT FILE

CLOSE THE DATABASE
CLOSE THE TEXT FILE


You need to be familiar with the mysql docs if you want to write a program that queries a mysql database.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

CAN YOU BE ABIT MORE SPECIFIC ON THIS PLEASE? I AM SORRY I AM QUITE NEW BUT I AM VERY INTERESTED..PLEASE~
:eek:

OPEN THE TEXT FILE FOR WRITE
OPEN THE DATABASE
QUERY THE DATABASE
CALL mysql_field_count() TO SEE IF THE QUERY RETURNED A RESULT SET
FOR EACH RESULTSET ROW (one of the mysql_fetch??? functions)
   WRITE RESULT ROW TO TEXT FILE
 
CLOSE THE DATABASE
CLOSE THE TEXT FILE


You need to be familiar with the mysql docs if you want to write a program that queries a mysql database.[/quote]

talyu
Newbie Poster
6 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 

You should at least be quite familiar with C/C++ before starting database I/O.

I'd start by reading some of the many online tutorials available out there. Once you begin to get the hang of it, you'll probably want to further your knowledge by reading the MySQL documentation as Ancient Dragon suggested.

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 
CAN YOU BE ABIT MORE SPECIFIC ON THIS PLEASE? I. .

Sorry, but you wouldn't understand even if I did post something more specific. Learn to crawl before you try to walk, and learn to walk before you try to run. When you know how to run then you will have little if any problems understanding how to do what you want.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You