problem to insert data

O

os2

hi

i have an insertion problem with my rtu table
datatype for date1 field is: datetime

i try to insert local date to the database (mysql)

source code

struct tm *date;
struct timeval begin;
struct timeval end;

gettimeofday(&end, NULL);
date = localtime(&end.tv_sec);
strftime(datestr, 128, "%F %T", date);
printf("date str %s", datestr);
snprintf(query, 512, "INSERT INTO rtu (date1,duration,sensorid)
VALUES(%s,%d,%d)",datestr, 0, 0);
res=mysql_query(&my_connection,query);

res display 1 (a error)

datestr display me
2004-11-25 15:52:55


query variable return
INSERT INTO rtu (date1,duration,sensorid) VALUES(2004-11-25
15:56:11,0,0)


any idea?
 
M

Merrill & Michele

os2 said:
hi

i have an insertion problem with my rtu table
datatype for date1 field is: datetime

i try to insert local date to the database (mysql)

source code

struct tm *date;
struct timeval begin;
struct timeval end;

gettimeofday(&end, NULL);
date = localtime(&end.tv_sec);
strftime(datestr, 128, "%F %T", date);
printf("date str %s", datestr);
snprintf(query, 512, "INSERT INTO rtu (date1,duration,sensorid)
VALUES(%s,%d,%d)",datestr, 0, 0);
res=mysql_query(&my_connection,query);

res display 1 (a error)

datestr display me
2004-11-25 15:52:55


query variable return
INSERT INTO rtu (date1,duration,sensorid) VALUES(2004-11-25
15:56:11,0,0)


any idea?
While I certainly don't possess the omniscience that the loudest voicest
around here do, I'm going to guess that some of your function calls lie
outside the ANSI/ISO Standard. If so, expect four more posts, progessively
shrill, telling you the same thing. MPJ
 
J

Jens.Toerring

os2 said:
i have an insertion problem with my rtu table
datatype for date1 field is: datetime
i try to insert local date to the database (mysql)
source code
struct tm *date;
struct timeval begin;
struct timeval end;
gettimeofday(&end, NULL);
date = localtime(&end.tv_sec);
strftime(datestr, 128, "%F %T", date);
printf("date str %s", datestr);
snprintf(query, 512, "INSERT INTO rtu (date1,duration,sensorid)
VALUES(%s,%d,%d)",datestr, 0, 0);
res=mysql_query(&my_connection,query);
res display 1 (a error)

That's not a C question at all, it's some SQL problem, so you better
ask in a group that deals with mysql or SQL. The only C related thing
I guess you should do is checking the return value of sprintf(),
which is the length of the string (without the trailing '\0') that
would have been needed to write the complete string, and compare
that to the buffer length - if the buffer was too short and the
text thus truncated you should not try to pass it as a command to
mysql.

query variable return
INSERT INTO rtu (date1,duration,sensorid) VALUES(2004-11-25
15:56:11,0,0)

Look at that again. You have a SQL syntax error - the string with
the date must be enlosed in single quotes (and are you sure that
you want to insert the data into _all_ records of the rtu table or
did you forget about the WHERE clause?) You better figure out how
to get at the error messages - but that's also not a C problem...
</OT>
Regards, Jens
 
D

Dave Thompson

Look at that again. You have a SQL syntax error - the string with
the date must be enlosed in single quotes (and are you sure that
you want to insert the data into _all_ records of the rtu table or
did you forget about the WHERE clause?) You better figure out how
to get at the error messages - but that's also not a C problem...

<stilll OT>
INSERT VALUES inserts a single record. INSERT SELECT inserts (copies)
a set, and SELECT, UPDATE, or DELETE without WHERE do all records.
I've seen several different syntaxes for datetime values, but none of
them and certainly not the standard is simply using single quotes. For
type char or varchar yes, but that's not what the OP said.

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,404
Latest member
PerryRutt

Latest Threads

Top