473,574 Members | 4,964 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4261
On Nov 6, 3:46*pm, Astley Le Jasper <Astley.lejas.. .@gmail.comwrot e:
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.comwrot e:
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_pri ce])/[recommended_pri ce]
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.dbap i2.Cursor object at 0xb7d6faa0>
>>con.executema ny("insert into foo(x) values (?)", [(3,), (3.0,)])
<pysqlite2.dbap i2.Cursor object at 0xb7d830e0>
>>print con.execute("se lect x from foo").fetchall( )
[(3,), (3,)] # <------------------------ !!!
>>con.execute(" create table bar(x real)")
<pysqlite2.dbap i2.Cursor object at 0xb7d83110>
>>con.executema ny("insert into bar(x) values (?)", [(3,), (3.0,)])
<pysqlite2.dbap i2.Cursor object at 0xb7d83170>
>>print con.execute("se lect 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.net com.comwrote:
On Fri, 07 Nov 2008 14:36:52 +0100, Gerhard Häring <g...@ghaering. de>
declaimed the following in comp.lang.pytho n:
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.netc om.com * * * * * * wulfr...@bestia ria.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_pri ce]) AS [difference]
([actual_price]-[recommended_pri ce])/[recommended_pri ce] AS
[difference_prop ortion]

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_pri ce],[difference],
[difference_prop ortion]
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
51010
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 field is float. The datatype for the other fields are int. I have tried testing by dividing 7 by 10000, which should give me 0.0007. The result...
6
2732
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 rational number class from the Boost libararies. Is there anything out there like that or for that matter in the framework that I am missing. Cheers
10
2668
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
1705
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. I went to The SQL Query Analyzer and executed the following T-SQL: SELECT field FROM table
8
5237
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" + " \+ " + ".05" + " \= " + (1.05 + .05) + "<br>"); document.write("2.05" + " \+ " + ".05" + " \= " + (2.05 + .05) +
9
12736
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 = {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2}; short int result = a / b; 'division without additional lib's and header files, only in standart C.
19
18366
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
2685
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. I don't know, how dotnet framework stored doubles, but it's certain, that if I store, say, 1.234567 in some real variable, it is stored in memory...
22
2748
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 cropped up before, since I rarely use "float"s for anything, and hardly ever use the function for floating-point numbers in the first place; I just was...
0
7821
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8083
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8258
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7835
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8121
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5635
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3761
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3777
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2255
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.