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:

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:

/* 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

foo(someParam, "my age is %d", age)

becouse the compiler thinks the function takes 3 parametars but at his definition it takes two.

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.

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);

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:

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

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:

char filename[255];
time_t now = time(0);
struct tm* tm = localtime(&now);
sprintf(filename,"%02d-02d-04d.txt", tm->tm_day, <snip> ...);

Hi all..

I've written a code something like this:

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);    
}

i try to compile this error but i've got this message..dunno how to solve it

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

hopefully anyone can help me..thanks

Is this a new question?

void set_day(struct tm* tb)
{
iDay = tb->tm_mday;
strftime(Day, sizeof(Day), "%a", tb);
}

Well, there's a problem in here, but I can't tell what. Where is iDay declared? Please post your entire (or relevant) code.

Edited 6 Years Ago by Nick Evan: Wasn't reading

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:

#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..

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..

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

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).

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  ;

That is wrong. mon is an ininitialized pointer that points to some random location in memory. Here is how to code that section

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;

Hi..thanks for the code..it's work..but then..the filename created is sumthing like this:

DATA-11-02d-02d-04s.txt

actually i already add for the hour section...supposedly the filename should be sumthing like this: DATA-HH-DD-MM-YYYY.txt...is there any missing tingy for the code?

Hi all,

I have another problem regarding this program..actually after this program write the data query from database into a filename, this program should update the status of data to show that the particular data have been written..i have write a funtion to update the status but it seems that, that function didnt funtion properly. below are the codes:

fp=fopen(filename, "a+");

        UpdateStatus(line2);
        bzero(buf, strlen(line2));
        strcpy (line2, "1");
        printf ("Test");

                  if ( (fputs(line2, fp)) == EOF) 
          {
                      printf("error write...");
          }
                  if ( (fputs("\n", fp)) == EOF) 
          {
                      printf("error write1...");
                  }

    
          //UpdateStatus(line2);

            fclose(fp);  
        
}

void UpdateStatus(char* MobileNo)
{
   MYSQL conn2;
   char query[2000];
   char query1[2000];
   char line2[2000];
   char string1[2000];

   mysql_init(&conn2);


   if (!mysql_real_connect(&conn2, host, user, password, database, 0, NULL, 0)) {
      fprintf(stderr, "%s\n", mysql_error(&conn2));
      exit(0);
   }

   sprintf(query1, "UPDATE Register SET Status = 1 WHERE MobileNumber = '%s'", MobileNo);

   if (strcmp(line2, "1") == 0) {
        if(mysql_query(&conn2,query1) != 0)
        {

                printf("Error Executing the query: %s\n\n",mysql_error(&conn2));
        }
   }
   mysql_close(&conn2);

}
This article has been dead for over six months. Start a new discussion instead.