Link to home
Start Free TrialLog in
Avatar of aiqing
aiqing

asked on

How to write current time into SYBASE database?

I want to write current time into SYBASE database and SQL ANYWHERE database by using ODBC... , my codes is somewhat like the following:
    time_t now;
    time(&now); // get current time
     ...
    SQLExeDirect(hsmt,"INSERT INTO TABLENAME(CURRENTTIME..) VALUES(now..)",SQL_NTS);
    but It cannot write successfully,so I hope some one could write correct codes for me!
    Thank you very much!
(PS: I'm using VC++5.0(SDK,not MFC) under Windows NT 4.0,and the Database are SYBASE SYSTEM 10 and SQL ANYWHERE provide with PB)
Avatar of david_levine
david_levine

When you say it isn't successfuly, what does that mean? Does it put something into the column, but not what you expected?

The easiest way to insert a time is using a string such as "12/13/98 12:27:30"

Can you try doing a sprintf to create the appropraite looking string?

Also, when specifying a value in a program that isn't a literal, you usually precede it with a :. In your example it would be VALUES(:NOW) though I'm not 100% sure about C and the SQLExeDirect funciton.

Let me know how it works.
David
Avatar of aiqing

ASKER


I do not want to write time to Database by using string,I want to
use the according Time STRUCTURE such as SYSTEMTIME,TIMESTAMP to
complete this function!
(PS:my method refered last time cannot work)
ASKER CERTIFIED SOLUTION
Avatar of GeoffS
GeoffS

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
aiqing,

Why don't you get a plain SQL statement to work the way you wantr to via isql or whatever front-end you use. Once you get it where it works, let me know the exact statement you used and I'll help again.

We only use strings to update date/time fields (or allow the database to do it automatically as GeoffS mentions).

David
Try this:
SQLExeDirect(hsmt,"INSERT INTO  
TABLENAME( your_date_field )
VALUES( getdate() )",SQL_NTS);