473,799 Members | 2,940 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 4268
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
51054
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 in the database is 0.
6
2745
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
2693
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
1713
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
5253
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
12746
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
18398
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
2699
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 like something close to 1.2345670000000000000003454786544 or...
22
2777
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 messing around testing it for all cases and noticed a problem.) Anyway, it is declared and I...
0
9541
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10225
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9072
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7564
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5463
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.