473,396 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

PySqlite - division of real numbers without decimal fractions

I've been getting errors recently when using pysqlite. I've declared
the table columns as real numbers to 2 decimal places (I'm dealing
with money), but when doing division on two numbers that happen to
have no decimal fractions, the results through pysqlite are coming
through as integers. The funny thing is that when looking at the
database using SQLite Manager or SQLite Pro the results there are
displayed correctly. As a temporary fix I've had to multiply one of
the numbers with 1.0 which has fixed it but I wonder if there is
something else I'm doing wrong.
Nov 6 '08 #1
4 4248
On Nov 6, 3:46*pm, Astley Le Jasper <Astley.lejas...@gmail.comwrote:
I've been getting errors recently when using pysqlite. I've declared
the table columns as real numbers to 2 decimal places (I'm dealing
with money), but when doing division on two numbers that happen to
have no decimal fractions, the results through pysqlite are coming
through as integers. The funny thing is that when looking at the
database using SQLite Manager or SQLite Pro the results there are
displayed correctly. As a temporary fix I've had to multiply one of
the numbers with 1.0 which has fixed it but I wonder if there is
something else I'm doing wrong.
You're using old-style division. Put the line "from __future__ import
division" in your script.
Nov 7 '08 #2
On Nov 7, 6:36*am, Dan Bishop <danb...@yahoo.comwrote:
On Nov 6, 3:46*pm, Astley Le Jasper <Astley.lejas...@gmail.comwrote:
I've been getting errors recently when using pysqlite. I've declared
the table columns as real numbers to 2 decimal places (I'm dealing
with money), but when doing division on two numbers that happen to
have no decimal fractions, the results through pysqlite are coming
through as integers. The funny thing is that when looking at the
database using SQLite Manager or SQLite Pro the results there are
displayed correctly. As a temporary fix I've had to multiply one of
the numbers with 1.0 which has fixed it but I wonder if there is
something else I'm doing wrong.

You're using old-style division. *Put the line "from __future__ import
division" in your script.
Hi,

But the calculations are being done within a sql statement within
SQLite?

([actual_price]-[recommended_price])/[recommended_price]
Nov 7 '08 #3
Astley Le Jasper wrote:
I've been getting errors recently when using pysqlite. I've declared
the table columns as real numbers to 2 decimal places (I'm dealing
with money),
MySQL doesn't have any MONEY type. All it has is INTEGER, REAL, TEXT,
BLOB and NULL types.
but when doing division on two numbers that happen to
have no decimal fractions, the results through pysqlite are coming
through as integers. The funny thing is that when looking at the
database using SQLite Manager or SQLite Pro the results there are
displayed correctly. As a temporary fix I've had to multiply one of
the numbers with 1.0 which has fixed it but I wonder if there is
something else I'm doing wrong.
Perhaps using SQLite's column affinity would help? Let the type be named
"real" instead of anything fancy:
>>from pysqlite2 import dbapi2 as sqlite3
con = sqlite3.connect(":memory:")
con.execute("create table foo(x number(10,2))")
<pysqlite2.dbapi2.Cursor object at 0xb7d6faa0>
>>con.executemany("insert into foo(x) values (?)", [(3,), (3.0,)])
<pysqlite2.dbapi2.Cursor object at 0xb7d830e0>
>>print con.execute("select x from foo").fetchall()
[(3,), (3,)] # <------------------------ !!!
>>con.execute("create table bar(x real)")
<pysqlite2.dbapi2.Cursor object at 0xb7d83110>
>>con.executemany("insert into bar(x) values (?)", [(3,), (3.0,)])
<pysqlite2.dbapi2.Cursor object at 0xb7d83170>
>>print con.execute("select x from bar").fetchall()
[(3.0,), (3.0,)] # <------------------------ !!!

Do you see the difference?

-- Gerhard

Nov 7 '08 #4
On 8 Nov, 05:39, Dennis Lee Bieber <wlfr...@ix.netcom.comwrote:
On Fri, 07 Nov 2008 14:36:52 +0100, Gerhard Häring <g...@ghaering.de>
declaimed the following in comp.lang.python:
Astley Le Jasper wrote:
I've been getting errors recently when using pysqlite. I've declared
the table columns as real numbers to 2 decimal places (I'm dealing
with money),
MySQL doesn't have any MONEY type. All it has is INTEGER, REAL, TEXT,
BLOB and NULL types.

* * * * Did you mean SQLite? <G>
Perhaps using SQLite's column affinity would help? Let the type be named
"real" instead of anything fancy:

* * * * If dealing with monetary computations, it might be betterto define
converters/adapters for Python's decimal type... Though that may mean
that doing simple SQL arithmetic may not be possible -- might need to
supply a Python function to work the arithmetic with conversion of the
data...
--
* * * * Wulfraed * * * *Dennis Lee Bieber * * * ** * * KD6MOG
* * * * wlfr...@ix.netcom.com * * * * * * wulfr...@bestiaria.com
* * * * * * * * HTTP://wlfraed.home.netcom.com/
* * * * (Bestiaria Support Staff: * * * * * * * web-a...@bestiaria.com)
* * * * * * * * HTTP://www.bestiaria.com/
Hi,

Sorry. I don't get this.

I am using numbers to 2dp (it doesn't really matter that it's money or
not) and importing them into SQLite where all the views are held. One
of the columns is doing the following calculations:
([actual_price]-[recommended_price]) AS [difference]
([actual_price]-[recommended_price])/[recommended_price] AS
[difference_proportion]

When using a SQLite gui like SQLiteManager I can see the imported data
is stored correctly and the column has been calculated correctly. So
I'll have something like:

[actual_price],[recommended_price],[difference],
[difference_proportion]
199.99,299.99,-100.00,-0.343344445
100.00,120.00,-100.00,-0.16666667

However, when calling the view from pysqlite I get the following
results
199.99,299.99,-100.00,-0.34
100.00,120.00,-100.00,0

So the row where both numbers have no decimal fraction are changing to
an integer. I looks like there is something going on in between sqlite
and pysqlite.
Nov 10 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: joel | last post by:
I have a table which I want to update by dividing one field into another. The update runs with no errors, but the results come out as only a positive integer number. The datatype for the result...
6
by: Mike Friel | last post by:
I am writing an assembly that will be used in a suite of financial applcations. Naturally I must use integer arithmetic to ensure the accuracy of my calculations and in the past have used the...
10
by: sp0 | last post by:
Is there a reason why to make mix numbers improper when adding? It seems when subtracting and adding, adding a subtracting the whole numbers and fraction parts should be sufficient? what'ch think
1
by: computer_prog | last post by:
I have a field of type Real in my SQL 2K database. I stored a value of ..35 in the field. When I "Open Table"->"Return All Rows" in the Enterprise Manager I get back .35 for the field value. ...
8
by: janice | last post by:
Hi all, I read a lot of info on this issue, but still don't get it. Why does this happen? document.write(".05" + " \+ " + ".05" + " \= " + (.05 + .05) + "<br>"); document.write("1.05" + "...
9
by: Marcin | last post by:
How I can make division of two numbers placed in arrays, example: short int a = {2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2}; short int b =...
19
by: Allagappan | last post by:
Hello everyone, Just check this code: #include<stdio.h> int main() { double a,b; scanf("%lf",&a); scanf("%lf",&b); printf("\nNumber a: %0.16lf",a);
10
by: Pavils Jurjans | last post by:
Hallo, It is know issue that due to the fact that computer has to store the real numbers in limited set of bytes, thus causing a minor imprecision from the decimal value that likely was stored....
22
by: Bill Reid | last post by:
I just noticed that my "improved" version of sscanf() doesn't assign floating point numbers properly if the variable assigned to is declared as a "float" rather than a "double". (This never...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.