Thread: Questions about INSERT INTO!

Questions about INSERT INTO!

From
"Anna Langer"
Date:
Hi!

Thanks for all help. It worked very well.

We still have some problems with inserting a datetime into the database.

If we write like this the database accept it:

PQexec(conn, "INSERT INTO test VALUES('23/01/1999 12:03:30');


When we are inserting other values in the database we write like this:

sprintf(command, "INSERT INTO octets VALUES(%d, %d)", a, b);

Now we wonder if we can put the date in to the database in the same way that 
we did with the integers? We have tried to make the date to a string, but it 
doesn't take it. The errormessage say that the database is of type datetime 
and the string is of type int4.

Is there anyone who can help us with this?

Thanks!

Anna and Maria


______________________________________________________
Get Your Private, Free Email at http://www.hotmail.com


Re: [INTERFACES] Questions about INSERT INTO!

From
Herouth Maoz
Date:
At 10:54 +0300 on 25/05/1999, Anna Langer wrote:


> Now we wonder if we can put the date in to the database in the same way that
> we did with the integers? We have tried to make the date to a string, but it
> doesn't take it. The errormessage say that the database is of type datetime
> and the string is of type int4.
>
> Is there anyone who can help us with this?
>
> Thanks!

You really must learn C. The trick with the %d is the standard way in C of
converting integers into strings. Dates are a bit more complicated. What is
the type you use for dates? time_t? If so, you convert it to string using
ctime(), a function in the standard C library. And then you use sprintf and
place it within single quotes.

It's true for every data type. You have to convert it to string, and then
have that string inserted into the command string using sprintf, with %s
within quotes. In the case of integer, the two steps can be shortened into
one, and you don't need quotes around the value. Same goes for float. Any
other data type needs the two steps and quotes.

Once you master this, remember that if your datatype is string, you have to
make sure every quote within the string is doubled (or preceded with a
backslash, but that's non-standard) before you pass it.

Herouth

--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma