Re: how to handle date in ESQL/C
Posted in 1995
I too found that direct email bounced. Actually, the problem is nothing to do with the type used for the DATE variables, because the Informix engines are pretty good at converting strings into other types. The real problem is the double quotes on the insert statement: $INSERT INTO request (state, fullname, login, cc, assigned, abstract ) VALUES ("$r_state", "$r_fullname", "$r_login", "$r_cc", "$r_assigned", "$r_abstract"); They should all be removed. $INSERT INTO request (state, fullname, login, cc, assigned, abstract ) VALUES ($r_state, $r_fullname, $r_login, $r_cc, $r_assigned, $r_abstract); What the original code was doing was doing was trying to insert a bunch of strings, each of which started with a $, int the database. That worked, of course, for the CHAR (or VARCHAR) columns, but not for the DATE columns. As a point of style, I'd strongly recommend not embedding the machine name where the database is found in the source code because it leads to problems when the machine is upgraded or otherwise changed. It is also a good idea to check whether you managed to connect to the database, but I assume you omitted that error checking when you sent the sample code. Yours, Jonathan Leffler (johnl@informix.com) #include <disclaimer.h> >From: bobb@netcom.com (EZ Travel) >Date: Wed, 14 Jun 1995 23:01:10 GMT >X-Informix-List-Id: <news.14684> > >[stuff deleted] > >Date is of type long and not char *. A simple assignment of var_type_long >= var_type_char won't do it. Look at todate() function in ctools.h or >also rstrdate() convert a string to an internal format. > >I tried sending you an example but the e-mail kicked back. [...] > >Bob Beaulieu >From: jaswant@highland.eng.octel.com (Jaswant Bhardwaj) >Date: 14 Jun 1995 18:43:45 GMT >X-Informix-List-Id: <news.14676> > >I have problem with the date field in the following program. When I >execute this, I get sqlcode of 1205 which says, invalid month in date. > > How do I declare 'r_now' and 'r_date_needed' variables so that the >esql accepts them. The value for r_now is 6/14/95 and r_date_needed >is 7/13/95. #include <stdio.h> $include sqlca; main (argc,argv) int argc; char *argv[]; { $char *r_req_no; $char *r_state ; $date *r_now; $date *r_date_needed; $char *r_fullname ; $char *r_login ; $char *r_cc ; $char *r_assigned ; $char *r_abstract; $database "//pyramid/request"; if ( argc < 9 ) { printf("%s\\n", "Usage: addreq <list of various fields>"); exit (0); } r_req_no=argv[1]; r_state=argv[2]; r_now=argv[3]; r_date_needed=argv[4]; r_fullname=argv[5]; r_login=argv[6]; r_cc=argv[7]; r_assigned=argv[8]; r_abstract=argv[9]; printf("r_req_no=%s\\n", r_req_no); printf("r_state=%s\\n", r_state); printf("r_now=%s\\n", r_now); printf("r_fullname=%s\\n", r_fullname); printf("r_login=%s\\n", r_login); printf("r_assigned=%s\\n", r_assigned); $insert into request (state, fullname, login, cc, assigned, abstract ) values ("$r_state", "$r_fullname", "$r_login", "$r_cc", "$r_assigned", "$r_abstract"); printf("sqlcode=%d\\n", sqlca.sqlcode); }