| | |
read from database and writing the contents into a text file
![]() |
•
•
Join Date: Jul 2006
Posts: 32
Reputation:
Solved Threads: 0
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..
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..

C Syntax (Toggle Plain Text)
#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; }
>>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
You need to be familiar with the mysql docs if you want to write a program that queries a mysql database.
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
C Syntax (Toggle Plain Text)
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.
Last edited by Ancient Dragon; Aug 24th, 2006 at 7:47 am.
•
•
Join Date: Jan 2007
Posts: 6
Reputation:
Solved Threads: 0
CAN YOU BE ABIT MORE SPECIFIC ON THIS PLEASE? I AM SORRY I AM QUITE NEW BUT I AM VERY INTERESTED..PLEASE~
:eek:
You need to be familiar with the mysql docs if you want to write a program that queries a mysql database.[/quote]
:eek:
C Syntax (Toggle Plain Text)
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]
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.
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."
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.
![]() |
Similar Threads
- Storing Text file in Database through VB (Visual Basic 4 / 5 / 6)
- C++ Reading from a text file (C++)
- Insert Contents Of Text File Into Database (ASP.NET)
- Please anybody to help to read string value in text file using C# (C#)
- read text file (C#)
- Reading in a text file string is not complete (Pascal and Delphi)
- Save a text file in an csv.file with ascii? (PHP)
Other Threads in the C Forum
- Previous Thread: a moving object
- Next Thread: EDOM: Domain Error
| Thread Tools | Search this Thread |
#include * adobe ansi array asterisks binarysearch centimeter changingto char character cm convert copyimagefile cprogramme creafecopyofanytypeoffileinc database dynamic execv feet fgets file floatingpointvalidation fork function getlogicaldrivestrin givemetehcodez global grade gtkwinlinux hacking histogram ide inches include incrementoperators infiniteloop input interest intmain() iso kernel keyboard kilometer license linked linkedlist linux list locate looping lowest match matrix meter microsoft number oddnumber opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc process program programming radix recursion recv recvblocked research reversing segmentationfault sequential single socket socketprograming socketprogramming standard strchr string suggestions systemcall test threads turboc unix urboc user variable voidmain() wab whythiscodecausesegmentationfault windowsapi






