Обсуждение: cast int to float

Поиск
Список
Период
Сортировка

cast int to float

От
"sheila timp"
Дата:
Hi,

I'm creating a new database-item by dividing two existing items. When these
items are initially declared as float, there is no problem. On the other
hand, when they are declared as int initially and later converted to float,
I get the error: float8div: divide by zero error. Maybe I didn't do the
casting right. This is what I did:

((cast(item1 as float))/(cast(item2 as float) + cast (item3 as float))) as
item4.

I played around with putting the brackets at different places, but every
time got the same error message.
Hopefully someone knows what I should do,

Thanks Sheila

_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp.


Re: cast int to float

От
Frank Bax
Дата:
Works for me...

fbax=# create table sheila (item1 int, item2 int, item3 int);
CREATE
fbax=# insert into sheila values (1, 2, 4);
INSERT 5971355 1
fbax=# select ((cast(item1 as float))/(cast(item2 as float) + cast (item3
as float))) as item4 from sheila;
       item4
-------------------
 0.166666666666667
(1 row)

Perhaps you have a row in your table where item2+item3 = 0?

Frank

At 10:26 PM 12/29/01 +0000, sheila timp wrote:
>Hi,
>
>I'm creating a new database-item by dividing two existing items. When these
>items are initially declared as float, there is no problem. On the other
>hand, when they are declared as int initially and later converted to float,
>I get the error: float8div: divide by zero error. Maybe I didn't do the
>casting right. This is what I did:
>
>((cast(item1 as float))/(cast(item2 as float) + cast (item3 as float))) as
>item4.
>
>I played around with putting the brackets at different places, but every
>time got the same error message.
>Hopefully someone knows what I should do,
>
>Thanks Sheila
>
>_________________________________________________________________
>Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp.
>
>
>---------------------------(end of broadcast)---------------------------
>TIP 3: if posting/reading through Usenet, please send an appropriate
>subscribe-nomail command to majordomo@postgresql.org so that your
>message can get through to the mailing list cleanly
>

Python, MacOSX & PostgreSQL

От
Nathan Wilson
Дата:
Is anyone else out there trying to get this combo to work?  Anyone know
anyone who might be working on this combo?

I got all the pieces compiled (Python 2.2b3, MacOSX 10.1, PostgreSQL
7.2b3, mx.DateTime 2.0.2).  Python and PostgreSQL are both working, but
when I try to connect to the DB from Python I get 'Connection refused' and
it asks if the server is accepting TCP/IP connections on port 5432.  If I
give psql that as the explicit port it does just fine.  I also tried
moving to a different port and changing the call to _pg.connect, but still
no joy.  Again psql does just fine.  BTW, I built Python as a Framework.

I've downloaded new latest and greatest (Python 2.2, MacOSX 10.1.2,
PostgreSQL 7.2b4, mx.DateTime 2.0.3), but I'm not terribly hopefully.

Oh, one other note, I have confirmed that python can handle sockets
by setting up pydoc as an http server and accessing it from Netscape.

Any suggestions are most welcome.

Thanks!
-Nathan



Re: Python, MacOSX & PostgreSQL

От
Tom Lane
Дата:
Nathan Wilson <velosa@cinenet.net> writes:
> I got all the pieces compiled (Python 2.2b3, MacOSX 10.1, PostgreSQL
> 7.2b3, mx.DateTime 2.0.2).  Python and PostgreSQL are both working, but
> when I try to connect to the DB from Python I get 'Connection refused' and
> it asks if the server is accepting TCP/IP connections on port 5432.  If I
> give psql that as the explicit port it does just fine.

Is it possible that in the Python case, PG sees the connection as coming
from your actual IP address and not from 127.0.0.1?  You might try
adding an entry to pg_hba.conf for your machine's IP address.
(Don't forget that 7.2 requires you to SIGHUP the postmaster to get it
to notice pg_hba.conf changes.)

            regards, tom lane

Re: Python, MacOSX & PostgreSQL

От
Nathan Wilson
Дата:
Thanks for the reply.  The problem was that I wasn't giving postmaster
the '-i' flag.  Thanks to Aarni for suggesting this solution.

Enjoy!
-Nathan

On Fri, 4 Jan 2002, Tom Lane wrote:

> Nathan Wilson <velosa@cinenet.net> writes:
> > I got all the pieces compiled (Python 2.2b3, MacOSX 10.1, PostgreSQL
> > 7.2b3, mx.DateTime 2.0.2).  Python and PostgreSQL are both working, but
> > when I try to connect to the DB from Python I get 'Connection refused' and
> > it asks if the server is accepting TCP/IP connections on port 5432.  If I
> > give psql that as the explicit port it does just fine.
>
> Is it possible that in the Python case, PG sees the connection as coming
> from your actual IP address and not from 127.0.0.1?  You might try
> adding an entry to pg_hba.conf for your machine's IP address.
> (Don't forget that 7.2 requires you to SIGHUP the postmaster to get it
> to notice pg_hba.conf changes.)
>
>             regards, tom lane
>



Bytea

От
"Daniel Cer"
Дата:
Could somebody give me an example of selects and inserts using the bytea
datatype? Is there any thorough documentation of this type online?

Thanks,

Dan


how to modify a field

От
"Terry BD7NQ"
Дата:
Hi Gentleman,

1. How to modify a field in the table structure, such I have a filed name
'userid', its type is serial, now I want to change ti to varchar?
2. How to delete a field in the table structure?

Terry


Re: how to modify a field

От
Jason Earl
Дата:
Unfortunately there isn't really a good way to do this in PostgreSQL.
What you are probably going to end up doing is creating a temporary
table with the new schema that you want, copying that data to this new
table with an 'insert into' query, delete the old table, and rename
the new table.

You will probably also want to get rid of the sequence object that
your serial type created, and heaven help you if you had a pile of
triggers set for this table.

Good Luck,
Jason

"Terry BD7NQ" <bd7nq@hellocq.net> writes:

> Hi Gentleman,
>
> 1. How to modify a field in the table structure, such I have a filed name
> 'userid', its type is serial, now I want to change ti to varchar?
> 2. How to delete a field in the table structure?
>
> Terry
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org