944,035 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 7359
  • C RSS
Aug 24th, 2006
0

read from database and writing the contents into a text file

Expand Post »
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. }
Similar Threads
Reputation Points: 16
Solved Threads: 0
Light Poster
whitemoss is offline Offline
32 posts
since Jul 2006
Aug 24th, 2006
0

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

>>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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,954 posts
since Aug 2005
Jan 22nd, 2007
0

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

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]
Reputation Points: 10
Solved Threads: 0
Newbie Poster
talyu is offline Offline
6 posts
since Jan 2007
Jan 22nd, 2007
0

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

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.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Jan 23rd, 2007
0

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

Click to Expand / Collapse  Quote originally posted by talyu ...
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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,954 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: a moving object
Next Thread in C Forum Timeline: EDOM: Domain Error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC