pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Richard Huxton <dev@archonet.com>
To: milimeter@163.com, "milimeter@163.com" <milimeter@163.com>
To: pgsql-general@postgresql.org
Subject: Re: jdbc problem
Date: Thu, 24 Apr 2003 10:15:27 +0100
Message-ID: <200304241015.28464.dev@archonet.com> (raw)
In-Reply-To: <200304240938.58666."milimeter@163.com">
References: <200304240938.58666."milimeter@163.com">

On Thursday 24 Apr 2003 2:38 am, milimeter@163.com wrote:
> Hi, all
>
>     I have a table: CREATE TABLE person (age char(4))
>     After executing "select age from person" with jdbc, I extract the value
> with:
>     getInt("age")
>     Now is the problem, the same code can succeed on oracle, but on
> postgresql, because of not triming space automaticlly, it cause an error.

If you don't want space-padded text, don't use char(), use varchar(). The 
char(4) column *always* contains four characters (space padded).

If for some reason you need to use char() types, then just trim it before 
applying getInt() or write your own function that can cope with trailing 
spaces.

>     So, I hope the newest jdbc driver for postgresql can automaticlly trim
> space just like oracle. The same problem also exists when execute insert:
>     Provided "age" is an integer, then "insert into person values ('30')"
> can be executed correctly, but "insert into person values ('')" can not. On
> oracle, a default value will be added. So jdbc driver for postgresql should
> also do it.

Oracle is probably treating the empty string as a null, which is where your 
default value is coming from. It shouldn't do this, since the empty string is 
not null.

Testing here, I can insert values of 30, '30', '' into a char(4) fine from the 
command-line. Ah! Unless where you say "age is an integer" you mean that the 
age column is of type integer, in which case PG can't translate an empty 
string into an integer (and nor can I). On the other hand, the string '30' 
can obviously be converted to an integer.

-- 
  Richard Huxton




view thread (13+ messages)  latest in thread

Message-ID: <200304241015.28464.dev@archonet.com>
Permalink:  ../200304241015.28464.dev@archonet.com/
Also on:    postgresql.org/message-id/200304241015.28464.dev@archonet.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: dev@archonet.com, milimeter@163.com
  Subject: Re: jdbc problem
  In-Reply-To: <200304241015.28464.dev@archonet.com>
  References: <200304240938.58666."milimeter@163.com"> <200304241015.28464.dev@archonet.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