Problems with INSERT INTO?

Started by Anna Langeralmost 27 years ago6 messagesgeneral
Jump to latest
#1Anna Langer
anna_langer@hotmail.com

Hi!
We are not really sure if this question is right for this mailinglist.
We have some problem with INSERT INTO and we dont know how to solve it.We
are writing it in a C-program. We are trying to get an integer from a file
and put it into a database. And we are sure that we get a interger into our
program.

When we created the table in the database we set the attribut to int4.
We get the problem when we trying to move the interger to the database.

int a=500;

PQexec(conn, "INSERT INTO octets VALUES(a)");

But the program works when we do like this:

PQexec(conn, "INSERT INTO octets VALUES(500)");

It feels like we need to convert int a in some way, byt we dont know how. We
are beginners with Postgresql.

/Anna and Maria

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

#2Herouth Maoz
herouth@oumail.openu.ac.il
In reply to: Anna Langer (#1)
Re: [GENERAL] Problems with INSERT INTO?

At 12:22 +0300 on 24/05/1999, Anna Langer wrote:

We are not really sure if this question is right for this mailinglist.
We have some problem with INSERT INTO and we dont know how to solve it.We
are writing it in a C-program. We are trying to get an integer from a file
and put it into a database. And we are sure that we get a interger into our
program.

The proper mailing list is the "interfaces" list, where I am redirecting
this post now.

int a=500;

PQexec(conn, "INSERT INTO octets VALUES(a)");

But the program works when we do like this:

PQexec(conn, "INSERT INTO octets VALUES(500)");

It feels like we need to convert int a in some way, byt we dont know how. We
are beginners with Postgresql.

This is less on the PostgreSQL side, and more on the C side. Just putting
"a" in a string doesn't tell C to pass the variable a. It just passes the
character "a", right?

What you have to do is pass the value of a as part of the command string.
So you have to make a string out of a. This is usually done with sprintf,
like this:

char command[1000];
int a=500;

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

PQexec( conn, command );

Herouth

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

#3Simon Drabble
simond@foxlink.net
In reply to: Anna Langer (#1)
Re: [GENERAL] Problems with INSERT INTO?

On Mon, 24 May 1999, Anna Langer wrote:

Hi!
We are not really sure if this question is right for this mailinglist.
We have some problem with INSERT INTO and we dont know how to solve it.We
are writing it in a C-program. We are trying to get an integer from a file
and put it into a database. And we are sure that we get a interger into our
program.

When we created the table in the database we set the attribut to int4.
We get the problem when we trying to move the interger to the database.

int a=500;

PQexec(conn, "INSERT INTO octets VALUES(a)");

But the program works when we do like this:

PQexec(conn, "INSERT INTO octets VALUES(500)");

It feels like we need to convert int a in some way, byt we dont know how. We
are beginners with Postgresql.

/Anna and Maria

Do this:

PQexec(conn, "INSERT INTO octets VALUES(%d)", a);

and read up on C's printf family of functions.

Simon.

--
On October 7, 1998 the routine process of running anti-virus software on a
Windows NT server at the Boeing Corporation shut the system down. Evidently,
the software identified Windows NT itself as a virus and disabled it.
-- http://www.vcnet.com/bmc/departments/nt/bugs.shtml

Simon Drabble Somewhere in cyberspace
simond@foxlink.net

#4Simon Drabble
simond@foxlink.net
In reply to: Simon Drabble (#3)
Re: [GENERAL] Problems with INSERT INTO?

On Mon, 24 May 1999, Simon Drabble wrote:

On Mon, 24 May 1999, Anna Langer wrote:

When we created the table in the database we set the attribut to int4.
We get the problem when we trying to move the interger to the database.

int a=500;

PQexec(conn, "INSERT INTO octets VALUES(a)");

But the program works when we do like this:

PQexec(conn, "INSERT INTO octets VALUES(500)");

It feels like we need to convert int a in some way, byt we dont know how. We
are beginners with Postgresql.

/Anna and Maria

Do this:

PQexec(conn, "INSERT INTO octets VALUES(%d)", a);

and read up on C's printf family of functions.

Simon.

*oops* heh-heh.

Do _this_:

char query[LOTS_OF_ROOM];
sprintf(query, "INSERT INTO octects VALUES(%d)", a);

PQexec(conn, query);

and read up etc..

-or- download my libpq wrapper, which allows you to perform queries without
using a temporary char array, in a manner similar to printf(). Available via
anon ftp from

brainkarma.dyndns.org/pub/dblib/

Note: it's very alpha right now and possibly quite messy as it's still under
development - works though :)

Simon.

--
On October 7, 1998 the routine process of running anti-virus software on a
Windows NT server at the Boeing Corporation shut the system down. Evidently,
the software identified Windows NT itself as a virus and disabled it.
-- http://www.vcnet.com/bmc/departments/nt/bugs.shtml

Simon Drabble Somewhere in cyberspace
simond@foxlink.net

#5Edwin S. Ramirez
ramirez@doc.mssm.edu
In reply to: Herouth Maoz (#2)
JDBC getQuote Call

Hello,

I am using the postgresql JDBC driver with a reporting application,
which calls getQuote. It seems that getQuote is not defined in the JDBC
driver.

Does anybody know what this is supposed to do?

-Edwin S. Ramirez-

#6Dustin Sallings
dustin@spy.net
In reply to: Simon Drabble (#4)
Re: [GENERAL] Problems with INSERT INTO?

On Mon, 24 May 1999, Simon Drabble wrote:

# char query[LOTS_OF_ROOM];
# sprintf(query, "INSERT INTO octects VALUES(%d)", a);
#
# PQexec(conn, query);

snprintf would probably be a better choice.

# -or- download my libpq wrapper, which allows you to perform queries without
# using a temporary char array, in a manner similar to printf(). Available via
# anon ftp from
#
# brainkarma.dyndns.org/pub/dblib/

I was about to suggest something like this be part of the core
library. It makes a lot of sense, and that's what vsnprintf is for. :)

--
SA, beyond.com My girlfriend asked me which one I like better.
pub 1024/3CAE01D5 1994/11/03 Dustin Sallings <dustin@spy.net>
| Key fingerprint = 87 02 57 08 02 D0 DA D6 C8 0F 3E 65 51 98 D8 BE
L_______________________ I hope the answer won't upset her. ____________