pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedINSERT query (using insert from a Java application, concerning String input)
4+ messages / 4 participants
[nested] [flat]
* INSERT query (using insert from a Java application, concerning String input)
@ 1999-02-17 21:10 Atika <agoswa@essex.ac.uk>
1999-02-18 00:22 ` Re: [SQL] INSERT query (using insert from a Java application, concerning String input) Brett W. McCoy <bmccoy@lan2wan.com>
0 siblings, 1 reply; 4+ messages in thread
From: Atika @ 1999-02-17 21:10 UTC (permalink / raw)
To: pgsql-sql
HI!!
I am hoping someone can help me with a problem I have.
I am trying to insert values in my pgsql database through a Java
application.
The problem is that I need to insert a String into a table. But, as I am
not going to know the value of this String before hand I can't really put
a value within two single qoutes.
I am basically trying to do something like this:
String input = "This is a String";
myConn.updateQuery("insert into myDB values(input)");
but get an error when I run it saying:
ERROR: attribute input not found
java.sql.SQLException: ERROR: attribute input not found
I am also going to have to insert integer and other such values but am not
going to know their values when I use the insert statement.
many thanks for your time and help.
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: [SQL] INSERT query (using insert from a Java application, concerning String input)
1999-02-17 21:10 INSERT query (using insert from a Java application, concerning String input) Atika <agoswa@essex.ac.uk>
@ 1999-02-18 00:22 ` Brett W. McCoy <bmccoy@lan2wan.com>
0 siblings, 0 replies; 4+ messages in thread
From: Brett W. McCoy @ 1999-02-18 00:22 UTC (permalink / raw)
To: Atika <agoswa@essex.ac.uk>; +Cc: pgsql-sql
On Wed, 17 Feb 1999, Atika wrote:
> String input = "This is a String";
>
> myConn.updateQuery("insert into myDB values(input)");
>
> but get an error when I run it saying:
> ERROR: attribute input not found
> java.sql.SQLException: ERROR: attribute input not found
>
> I am also going to have to insert integer and other such values but am not
> going to know their values when I use the insert statement.
I don't know the details of the Java interface (perhaps you should pose
your question to the interfaces list?), but you still need the single
quotes else the term ends up in the SQL querytree as an attribute rather
than as data to insert into an attribute. All of the language interfaces
I have used still require the single quotes, even if you are
interpolating string variables inside of the SQL statement. Or is the
JDBC completely different in this? Can you prepare your query string
beforehand and then pass the entire string as a varaible rather than as
a literal to the updateQuery method?
Brett W. McCoy
http://www.lan2wan.com/~bmccoy
-----------------------------------------------------------------------
Labor, n.:
One of the processes by which A acquires property for B.
-- Ambrose Bierce, "The Devil's Dictionary"
----- BEGIN GEEK CODE BLOCK -----
Version: 3.12
GAT dpu s:-- a C++++ UL++++$ P+ L+++ E W++ N- o K- w--- O@ M-@ !V PS+++
PE Y+ PGP- t++ 5- X+ R+@ tv b+++ DI+++ D+ e>++ h+ r++ y++++
------ END GEEK CODE BLOCK ------
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: [SQL] INSERT query (using insert from a Java application, concerning String input)
@ 1999-02-18 00:46 Peter Garner <peter_garner@yahoo.com>
1999-02-21 15:45 ` Re: [SQL] INSERT query (using insert from a Java application, concerning String input) Herouth Maoz <herouth@oumail.openu.ac.il>
0 siblings, 1 reply; 4+ messages in thread
From: Peter Garner @ 1999-02-18 00:46 UTC (permalink / raw)
To: Atika <agoswa@essex.ac.uk>; pgsql-sql
Hi Atika! :-)
> I am basically trying to do something like this:
>
> String input = "This is a String";
>
> myConn.updateQuery("insert into myDB values(input)");
>
> but get an error when I run it saying:
> ERROR: attribute input not found
> java.sql.SQLException: ERROR: attribute input not found
Try
String input = "This is a String"
String query = insert into myDB value ('" + input + "')"
myConn.updateQuery(query);
==
Peace,
Peter
We are Microsoft of Borg, you will be assimilated!!!
Resistance is fut... ***BZZZRT*** THUD!!!
[General Protection Fault in MSBorg32.DLL]
Please contact the vendor for more information
_________________________________________________________
DO YOU YAHOO!?
Get your free @yahoo.com address at http://mail.yahoo.com
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: [SQL] INSERT query (using insert from a Java application, concerning String input)
1999-02-18 00:46 Re: [SQL] INSERT query (using insert from a Java application, concerning String input) Peter Garner <peter_garner@yahoo.com>
@ 1999-02-21 15:45 ` Herouth Maoz <herouth@oumail.openu.ac.il>
0 siblings, 0 replies; 4+ messages in thread
From: Herouth Maoz @ 1999-02-21 15:45 UTC (permalink / raw)
To: pgsql-interfaces@postgresql.org; +Cc: Atika <agoswa@essex.ac.uk>; Peter Garner <peter_garner@yahoo.com>
(Continue this thread on the INTERFACES list, where it belongs)
At 2:46 +0200 on 18/2/99, Peter Garner wrote:
> Hi Atika! :-)
>
> > I am basically trying to do something like this:
> >
> > String input = "This is a String";
> >
> > myConn.updateQuery("insert into myDB values(input)");
> >
> > but get an error when I run it saying:
> > ERROR: attribute input not found
> > java.sql.SQLException: ERROR: attribute input not found
>
> Try
>
> String input = "This is a String"
> String query = insert into myDB value ('" + input + "')"
>
> myConn.updateQuery(query);
Ahem. No, this may work, but that's not the way to go about it. In any
case, where did that "updateQuery" come from?
Atika, I have a feeling you are not well-acquainted with JDBC. Reccomended
reading:
http://www.javasoft.com/products/jdk/1.2/docs/guide/jdbc/getstart/introTOC.doc.h
tml
(All on the same line. Not my fault the URLs are that long at Javasoft).
The basic idea is to use a PreparedStatement. These statements include
placeholders, which you fill before you execute them. This enables you to
(a) reuse the same statements with different values, (b) use things other
than strings and (c) have the strings properly quoted and escaped for you.
Thus:
PreparedStatement stmt =
myConn.prepareStatement( "INSERT INTO myDB VALUES ( ? ) " );
String input = "This is a string";
stmt.setString( 1, input );
stmt.executeUpdate();
Note that Peter's solution would not have worked if your input was:
String input = "It's a beautiful day today."
Herouth
--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~1999-02-21 15:45 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
1999-02-17 21:10 INSERT query (using insert from a Java application, concerning String input) Atika <agoswa@essex.ac.uk>
1999-02-18 00:22 ` Brett W. McCoy <bmccoy@lan2wan.com>
1999-02-18 00:46 Re: [SQL] INSERT query (using insert from a Java application, concerning String input) Peter Garner <peter_garner@yahoo.com>
1999-02-21 15:45 ` Herouth Maoz <herouth@oumail.openu.ac.il>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox