read from database and writing the contents into a text file

Reply

Join Date: Jul 2006
Posts: 32
Reputation: whitemoss is an unknown quantity at this point 
Solved Threads: 0
whitemoss whitemoss is offline Offline
Light Poster

read from database and writing the contents into a text file

 
0
  #1
Aug 24th, 2006
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..

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <mysql.h>
  4.  
  5. #define HOST "localhost"
  6. #define USER "yati"
  7. #define PASSWD "yati"
  8. #define DB_NAME "subscriberTool"
  9.  
  10. void openFile();
  11. void readFromDB();
  12.  
  13. static MYSQL demo_db;
  14. //char hp[50], startdate[50], enddate[50];
  15.  
  16. int main()
  17. {
  18. openFile();
  19. }
  20.  
  21.  
  22. void openFile()
  23. {
  24.  
  25. char line[1600];
  26. char lineone[1600];
  27. char hp[50], startdate[50], enddate[50];
  28. FILE *fp;
  29.  
  30. fp=fopen("TH.txt", "w");
  31.  
  32. if(fp==NULL)
  33. {
  34. printf("file cannot be opened!\n");
  35. exit(0);
  36. }
  37. else {
  38.  
  39. while(!feof(fp))
  40. {
  41. bzero(line, sizeof(line));
  42. fgets(line,1600,fp);
  43. readFromDB();
  44. sprintf(lineone, "%s,%s,%s", hp, startdate, enddate);
  45. printf("Error");
  46. }
  47.  
  48.  
  49. }
  50.  
  51.  
  52. fclose(fp);
  53. return;
  54. }
  55.  
  56. void readFromDB()
  57. {
  58. char query[16384];
  59. char query1[16384];
  60.  
  61.  
  62. if(!mysql_connect(&demo_db, HOST, USER, PASSWD))
  63. {
  64. printf(mysql_error(&demo_db));
  65. exit(1);
  66. }
  67.  
  68. if(mysql_select_db(&demo_db, DB_NAME))
  69. {
  70. /* Select the database we want to use */
  71. printf(mysql_error(&demo_db));
  72. exit(1);
  73. }
  74.  
  75. bzero(query, sizeof(query1));
  76.  
  77. sprintf(query, "SELECT MobileNumber,StartDate,EndDate FROM Subscriptions order by StartDate asc");
  78. mysql_query(&demo_db, query1);
  79.  
  80. printf("%s\n",query);
  81.  
  82. if(mysql_real_query(&demo_db, query, strlen(query)+255))
  83. {
  84. printf(mysql_error(&demo_db));
  85. bzero(query, sizeof(query));
  86. exit(1);
  87. }
  88.  
  89. mysql_close(&demo_db);
  90.  
  91. return;
  92. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,362
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: read from database and writing the contents into a text file

 
0
  #2
Aug 24th, 2006
>>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
  1. OPEN THE TEXT FILE FOR WRITE
  2. OPEN THE DATABASE
  3. QUERY THE DATABASE
  4. CALL mysql_field_count() TO SEE IF THE QUERY RETURNED A RESULT SET
  5. FOR EACH RESULTSET ROW (one of the mysql_fetch??? functions)
  6. WRITE RESULT ROW TO TEXT FILE
  7.  
  8. CLOSE THE DATABASE
  9. 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.
Last edited by Ancient Dragon; Aug 24th, 2006 at 7:47 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 6
Reputation: talyu is an unknown quantity at this point 
Solved Threads: 0
talyu talyu is offline Offline
Newbie Poster

Re: read from database and writing the contents into a text file

 
0
  #3
Jan 22nd, 2007
CAN YOU BE ABIT MORE SPECIFIC ON THIS PLEASE? I AM SORRY I AM QUITE NEW BUT I AM VERY INTERESTED..PLEASE~
:eek:
  1. OPEN THE TEXT FILE FOR WRITE
  2. OPEN THE DATABASE
  3. QUERY THE DATABASE
  4. CALL mysql_field_count() TO SEE IF THE QUERY RETURNED A RESULT SET
  5. FOR EACH RESULTSET ROW (one of the mysql_fetch??? functions)
  6. WRITE RESULT ROW TO TEXT FILE
  7.  
  8. CLOSE THE DATABASE
  9. 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]
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: read from database and writing the contents into a text file

 
0
  #4
Jan 22nd, 2007
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.
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,362
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: read from database and writing the contents into a text file

 
0
  #5
Jan 23rd, 2007
Originally Posted by talyu View Post
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.
Last edited by Ancient Dragon; Jan 23rd, 2007 at 12:08 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC