| | |
error: too many arguments to function `mysql_query'
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2006
Posts: 32
Reputation:
Solved Threads: 0
Hi all,
I try to compile my program and got this kind of error:
test6.c: In function `main':
test6.c:46: too many arguments to function `mysql_query'
Here is my code:
i try to alter the code to become like this:
at the end..when i try to run the program, i got "segmentation fault"
Please help me..many thanks
I try to compile my program and got this kind of error:
test6.c: In function `main':
test6.c:46: too many arguments to function `mysql_query'
Here is my code:
C Syntax (Toggle Plain Text)
int main(int argc, char *argv[]) { char string1[1600], string2[1600], query[1600], mobileno[50]; int i, j, len; FILE *fp2; mysql_init(&conn); /* Connect to database */ if (!mysql_real_connect(&conn, host, user, password, database, 0, NULL, 0)) { fprintf(stderr, "%s\n", mysql_error(&conn)); exit(0); } /* send SQL query */ if (mysql_query(&conn, "select a.MobileNumber, a.MasterNumber , a.MasterAccNo, b.Addr, b.Addr2, b.Addr3, b.Addr4, b.Addr5, b.Addr6, b.Addr7, b.Addr8, b.Postcode, b.CountryCode, b.BillName, b.CorrName, b.ICNew, b.ICOld, b.Passport from Register as a, Profile as b where a.MasterNumber = b.Number and a.Status=0 and a.MasterNumber = '%s'", masterno)) { fprintf(stderr, "%s\n", mysql_error(&conn)); exit(0); } res = mysql_use_result(&conn); /* output fields 0, 1 and 2 of each row */ while ((row = mysql_fetch_row(res)) != NULL) { if (i==11) { timebuffer(); i=1; bzero(string1, sizeof(string1)); } else { sprintf(string1,"%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|", row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15], row[16], row[17], row[18]); printf ("%s\n",string1); writeLogFile(string1, mobileno); i++; } } /* Release memory used to store results and close connection */ mysql_free_result(res); mysql_close(&conn); return 0; }
i try to alter the code to become like this:
C Syntax (Toggle Plain Text)
/* send SQL query */ bzero(query1, sizeof(query1)); sprintf(query1, "select a.MobileNumber, a.MasterNumber , a.MasterAccNo, b.Addr, b.Addr2, b.Addr3, b.Addr4, b.Addr5, b.Addr6, b.Addr7, b.Addr8, b.Postcode, b.CountryCode, b.BillName, b.CorrName, b.ICNew, b.ICOld, b.Passport from Register as a, Profile as b where a.MasterNumber = b.Number and a.MasterNumber = '%s' and a.Status=0", MasterNo); printf ("%s\n",query1); if (mysql_query(&conn, query1)) { fprintf(stderr, "%s\n", mysql_error(&conn)); exit(0); }
at the end..when i try to run the program, i got "segmentation fault"
Please help me..many thanks
You cant do this
becouse the compiler thinks the function takes 3 parametars but at his definition it takes two.
C Syntax (Toggle Plain Text)
foo(someParam, "my age is %d", age)
Last edited by andor; Nov 13th, 2006 at 6:38 am.
If you want to win, you must not loose (Alan Ford)
For your own santity why don't you break up that HUGE string into several lines to make it easier for you to read. Example: Note that each line starts and ends with quotes and does not have a line terminator semicolon.
C Syntax (Toggle Plain Text)
sprintf(query1, "select a.MobileNumber, a.MasterNumber ," "a.MasterAccNo, b.Addr, b.Addr2, b.Addr3, b.Addr4, b.Addr5, b.Addr6," "b.Addr7, b.Addr8, b.Postcode, b.CountryCode, b.BillName, b.CorrName," "b.ICNew, b.ICOld, b.Passport from Register as a, Profile as b" " where a.MasterNumber = b.Number and a.MasterNumber = '%s' and" " a.Status=0", MasterNo);
Last edited by Ancient Dragon; Nov 13th, 2006 at 7:29 am.
I told Santa what I wanted for Christmas and he washed my mouth out with soap.
•
•
Join Date: Jul 2006
Posts: 32
Reputation:
Solved Threads: 0
Hi all,
Thanks for ur reply....the above problem has been solved...
i have another thing..hopefully u all can help me. as u all can see, i have written a writeLogFile function where it appends the data select from db to a file namely data.txt...but i would like to change it so that, i dont need to specify the filename..instead, this function can appends the data into a different file based on when the data was appended. Actually i have created a cron job to run this program by hourly so..the filename should be on hourly basis..For example at 10 am on 14/11/2006...this program is running and it appends the data from database to a file namely data-10-14-11-2006.txt...1 hr later, it appends another data to other file namely data-11-14-11-2006.txt and so on... how to expand this function so that it can cater as what i've mentioned before..
below is the code for writeLogFile function:
ur help is very much appreciated...Many thanks
Thanks for ur reply....the above problem has been solved...
i have another thing..hopefully u all can help me. as u all can see, i have written a writeLogFile function where it appends the data select from db to a file namely data.txt...but i would like to change it so that, i dont need to specify the filename..instead, this function can appends the data into a different file based on when the data was appended. Actually i have created a cron job to run this program by hourly so..the filename should be on hourly basis..For example at 10 am on 14/11/2006...this program is running and it appends the data from database to a file namely data-10-14-11-2006.txt...1 hr later, it appends another data to other file namely data-11-14-11-2006.txt and so on... how to expand this function so that it can cater as what i've mentioned before..
below is the code for writeLogFile function:
C Syntax (Toggle Plain Text)
void writeLogFile(char line2[1600], char* MobileNo) { FILE *fp; fp=fopen("data.txt", "a+"); if ( (fputs(line2, fp)) == EOF) { printf("error write..."); } if ( (fputs("\n", fp)) == EOF) { printf("error write1..."); } fclose(fp); }
ur help is very much appreciated...Many thanks
Last edited by whitemoss; Nov 13th, 2006 at 12:30 pm.
I have a logging program that changes the filename every day. Whenever the log function is called it creates the filename based on the current system time. Example:
C Syntax (Toggle Plain Text)
char filename[255]; time_t now = time(0); struct tm* tm = localtime(&now); sprintf(filename,"%02d-02d-04d.txt", tm->tm_day, <snip> ...);
Last edited by Ancient Dragon; Nov 13th, 2006 at 1:08 pm.
I told Santa what I wanted for Christmas and he washed my mouth out with soap.
•
•
Join Date: Jul 2006
Posts: 32
Reputation:
Solved Threads: 0
Hi all..
I've written a code something like this:
i try to compile this error but i've got this message..dunno how to solve it
hopefully anyone can help me..thanks
I've written a code something like this:
C Syntax (Toggle Plain Text)
void get_year(char* year) { strcpy(year, Year); } void get_month(char* month) { strcpy(month, Month); } void get_day(char* day) { strcpy(day, Day); } void set_year(struct tm* tb) { memset(Year, '\0', sizeof(Year)); strftime(Year, sizeof(Year), "%Y", tb); iYear = atoi(Year); } void set_day(struct tm* tb) { iDay = tb->tm_mday; strftime(Day, sizeof(Day), "%a", tb); } void set_month(struct tm* tb) { iMonth = tb->tm_mon + 1; memset(Month, '\0', sizeof(Month)); strftime(Month, sizeof(Month), "%b", tb); } void set_systime() { /* gets time of day */ now = time(NULL); /* converts date/time to a structure */ struct tm* tb = localtime(&now); set_day(now); set_month(now); set_year(now); } void writeLogFile(char line2[1600], char* MobileNo) { char Temp[55]; char Year[5]; char filename[255], HourlyFileName[500]; memset(filename, '\0', sizeof(filename)); sprintf (filename, "%02d-%02d-%04s.log", pTime->get_day(), pTime->get_month(), pTime->get_Year()); FILE *fp; fp=fopen(filename, "a+"); if ( (fputs(line2, fp)) == EOF) { printf("error write..."); } if ( (fputs("\n", fp)) == EOF) { printf("error write1..."); } fclose(fp); printf ("%s\n",filename); }
C Syntax (Toggle Plain Text)
test8.c: In function `set_year': test8.c:140: incompatible types in assignment test8.c: In function `set_day': test8.c:145: incompatible types in assignment test8.c: In function `set_month': test8.c:152: incompatible types in assignment test8.c: In function `set_systime': test8.c:162: incompatible types in assignment test8.c:165: warning: passing arg 1 of `localtime' from incompatible pointer type test8.c: In function `writeLogFile': test8.c:219: dereferencing pointer to incomplete type test8.c:220: dereferencing pointer to incomplete type test8.c:221: dereferencing pointer to incomplete type
Last edited by whitemoss; Nov 17th, 2006 at 4:07 am.
Is this a new question?
Well, there's a problem in here, but I can't tell what. Where is iDay declared? Please post your entire (or relevant) code.
regards Niek
•
•
•
•
Originally Posted by whitemoss
void set_day(struct tm* tb)
{
iDay = tb->tm_mday;
strftime(Day, sizeof(Day), "%a", tb);
}
regards Niek
Last edited by niek_e; Nov 17th, 2006 at 4:51 am. Reason: Wasn't reading
•
•
Join Date: Jul 2006
Posts: 32
Reputation:
Solved Threads: 0
Hi all,
i've changed my code and it's more simpler than before..I already can compile the code but then, when i'm trying to run it, i've got the "Segmentation Fault" error.
The code is as below:
This is the output when I try to run the program..
i've changed sumthing to the coding. i just changed sprintf(filename,"DATA-%02d-02d-04s.txt", ..) and I've got this when running it..
it seems like, there is sumthing wrong with filename..and no file actually was created..
Many thanks
i've changed my code and it's more simpler than before..I already can compile the code but then, when i'm trying to run it, i've got the "Segmentation Fault" error.
The code is as below:
C Syntax (Toggle Plain Text)
#include <mysql.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <time.h> MYSQL conn; MYSQL_RES *res; MYSQL_ROW row; char *host = "localhost"; char *user = "External"; char *password = "prepaidsms"; char *database = "External"; int tbuf; char* buf; char *line; char MasterNo[50]; void writeLogFile(char line2[1600], char* MobileNo); void UpdateStatus(char* MobileNo); void timebuffer(); int main(int argc, char *argv[]) { char string1[1600], string2[1600], query[1600], query1[1600], mobileno[50]; int i, j, len; FILE *fp2; mysql_init(&conn); /* Connect to database */ if (!mysql_real_connect(&conn, host, user, password, database, 0, NULL, 0)) { fprintf(stderr, "%s\n", mysql_error(&conn)); exit(0); } /* send SQL query */ bzero(query1, sizeof(query1)); sprintf(query1, "select a.MobileNumber, a.MasterNumber, a.MasterAccNo, b.Addr, b.Addr2, b.Addr3, b.Addr4, b.Addr5, b.Addr6," "b.Addr7, b.Addr8, b.Postcode, b.CountryCode, b.BillName, b.CorrName, b.ICNew, b.ICOld, b.Passport from Register as a, Profile as b" " where a.MasterNumber = b.Number and a.Status=0"); //printf ("%s\n",query1); if (mysql_query(&conn, query1)) { fprintf(stderr, "%s\n", mysql_error(&conn)); exit(0); } res = mysql_use_result(&conn); /* output fields 0, 1 and 2 of each row */ while ((row = mysql_fetch_row(res)) != NULL) { if (i==11) { timebuffer(); i=1; bzero(string1, sizeof(string1)); } else { sprintf(string1,"%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|", row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15], row[16], row[17], row[18]); printf ("%s\n",string1); writeLogFile(string1, mobileno); i++; } } /* Release memory used to store results and close connection */ mysql_free_result(res); mysql_close(&conn); return 0; } void writeLogFile(char line2[1600], char* MobileNo) { char Temp[55]; char Year[5]; char filename[255]; char *mon; char *day; char *yr; time_t tvec; struct tm* dtime; time(&tvec); dtime = localtime(&tvec); *mon = dtime->tm_mon + 1 ; *day = dtime->tm_mday ; *yr = dtime->tm_year ; sprintf(filename,"DATA-%02d-02d-04d.txt", mon, day, yr); // Filename Format YYYY-MM-DD printf ("%s\n",filename); FILE *fp; fp=fopen(filename, "a+"); if ( (fputs(line2, fp)) == EOF) { printf("error write..."); } if ( (fputs("\n", fp)) == EOF) { printf("error write1..."); } fclose(fp); }
This is the output when I try to run the program..
C Syntax (Toggle Plain Text)
019xxxxxxx|019xxxxxxx|54344770|Customer Service|7TH FLOOR, MENARA CELCOM|JALAN RAJA MUDA ABDUL AZIZ|Kuala Lumpur|W PERSEKUTUAN KUALA LUMPUR||||50300||KHAIRUL BARIAH BINTI NORDIN||||| Segmentation fault
i've changed sumthing to the coding. i just changed sprintf(filename,"DATA-%02d-02d-04s.txt", ..) and I've got this when running it..
C Syntax (Toggle Plain Text)
0192744210|099122844|d217585300104|LOT 6, ,|KUBANG KAWAH,|BACHOK|16300 PERUPOK|||||16300|MAL|AB WAHAB B MOHD ARIFF|AB WAHAB B MOHD ARIFF|571214-04-5423|5256704|| DATA--1218443020-02d-04s.txt Segmentation fault
it seems like, there is sumthing wrong with filename..and no file actually was created..
Many thanks
Last edited by whitemoss; Nov 20th, 2006 at 10:45 pm.
the result set has 18 columns on each row, you sprintf() statement is attempting to use 19 columns (row[0] thru row[18] is 19 columns).
That is wrong. mon is an ininitialized pointer that points to some random location in memory. Here is how to code that section
C Syntax (Toggle Plain Text)
char *mon; char *day; char *yr; time_t tvec; struct tm* dtime; time(&tvec); dtime = localtime(&tvec); *mon = dtime->tm_mon + 1 ; *day = dtime->tm_mday ; *yr = dtime->tm_year ;
C Syntax (Toggle Plain Text)
int mon; int day; int yr; time_t tvec; struct tm* dtime; time(&tvec); dtime = localtime(&tvec); mon = dtime->tm_mon + 1 ; day = dtime->tm_mday ; yr = dtime->tm_year +1900;
Last edited by Ancient Dragon; Nov 20th, 2006 at 11:35 pm.
I told Santa what I wanted for Christmas and he washed my mouth out with soap.
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: copy constructor and 2 args constructor help
- Next Thread: decompiler won't compile!
Views: 3475 | Replies: 11
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char command convert copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax directory dynamic executable fflush file fork forloop frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix problem probleminc program programming radix recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socketprograming spoonfeeding stack standard string strings structures student systemcall testautomation turboc unix user variable visualstudio voidmain() wab win32 windows.h






