pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Amn Ojee Uw <amnojeeuw@gmail.com>
To: pgsql-general@lists.postgresql.org
Subject: PSQL = Yes ... JDBC = no ??
Date: Sun, 3 Sep 2023 12:00:22 -0400
Message-ID: <cbe8bd8f-7d34-29b3-a169-9cefb9f1de83@gmail.com> (raw)

Hello!

This issue really puzzles me beyond intrigue.

Why would this schema:
*SELECT 'CREATE DATABASE jme_test_database' WHERE NOT EXISTS (SELECT 
FROM pg_database WHERE datname = 'jme_test_database')\gexec*
work when entered at the psql prompt, but not when passed as a parameter 
in a JDBC method?

The bellow snip demonstrates the the creation of a String object with 
same character string used in the PSQL#.
//CREATE DATABASE IF NOT EXISTS
//~~~~~~~~~~~~~~~~~~
*var s = new ***myString*(SELECT 'CREATE DATABASE jme_test_database' 
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 
'jme_test_database')\gexec);**
**
**stmt.executeUpdate(s.toString());*

However, this time PostgreSQL-15 complains with an error message saying :

*Exception in thread "main" org.postgresql.util.PSQLException: ERROR: 
syntax error at or near "\"**
**  Position: 122

*

For better clarification, I'd like to show the code to create the schema 
inside Java/JDBC.
---- snip ----

  */
    ...

     var sv = myString();
     ...

     public final myString getCreateDatabase(myString s){
         this.sv.setData("SELECT 'CREATE DATABASE ");
         this.sv.append(s.toString());
         this.sv.append("\'");
         this.sv.append("WHERE NOT EXISTS (SELECT FROM pg_database WHERE 
datname = ");
         this.sv.append("\'");
         this.sv.append(s.toString());
         this.sv.append("\'");
         this.sv.append(")\\gexec");

         return sv;
     }


Any help would be very much appreciated.

Thanks in advance.

view thread (8+ messages)  latest in thread

Message-ID: <cbe8bd8f-7d34-29b3-a169-9cefb9f1de83@gmail.com>
Permalink:  ../cbe8bd8f-7d34-29b3-a169-9cefb9f1de83@gmail.com/
Also on:    postgresql.org/message-id/cbe8bd8f-7d34-29b3-a169-9cefb9f1de83@gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: amnojeeuw@gmail.com
  Subject: Re: PSQL = Yes ... JDBC = no ??
  In-Reply-To: <cbe8bd8f-7d34-29b3-a169-9cefb9f1de83@gmail.com>
  References: <cbe8bd8f-7d34-29b3-a169-9cefb9f1de83@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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