SQLite Real Number Issue

Everything else that doesn't fall into one of the other PB categories.
swhite
Enthusiast
Enthusiast
Posts: 727
Joined: Thu May 21, 2009 6:56 pm

SQLite Real Number Issue

Post by swhite »

Hi

Since SQLite does not assign a type to a column I have an issue where a column that I want to always be a real number sometimes is returned as an Integer. For example 50.01 is a real value and returned as real by SQLite but 50.00 is a real value but it is stored in SQLite as 50 and then returned as an integer. So when I want to use the PB GetDatabaseDouble() I do not get a value return instead I have to use GetDatabaseLong(). Currently I am checking the column type every time I want to get the value from the database but I wondered if there is a better scenario to ensure the value is correctly read from the data base.

Thanks,
Simon
Simon White
dCipher Computing
User avatar
skywalk
Addict
Addict
Posts: 4003
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: SQLite Real Number Issue

Post by skywalk »

Don't understand your question?
Open your database in a db browser and inspect the table schema.
Or, if you are the creator, make sure to assign a Double to your desired columns.
CREATE TABLE Keywords (Word TEXT, Item TEXT, img BLOB, `X(dB)` DOUBLE);
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
deeproot
Enthusiast
Enthusiast
Posts: 271
Joined: Thu Dec 17, 2009 12:00 pm
Location: Llangadog, Wales, UK
Contact:

Re: SQLite Real Number Issue

Post by deeproot »

I've been using large SQLite databases with PureBasic for years and never had a problem with Real versus Integer columns!
swhite wrote:For example 50.01 is a real value and returned as real by SQLite but 50.00 is a real value but it is stored in SQLite as 50 and then returned as an integer.
Not the whole story - while SQLite allows storing any type in most columns, it also assigns a "type affinity" to each column which can distinguish between Real or Integer.

See https://www.sqlite.org/datatype3.html

skywalk's advice is good - CREATE TABLE determines the type affinity.
swhite
Enthusiast
Enthusiast
Posts: 727
Joined: Thu May 21, 2009 6:56 pm

Solved: SQLite Real Number Issue

Post by swhite »

Hi

After further investigation I found the source of the problem. The column had been created with "num" type and not the "real" type. Once I changed the column to real everything worked correctly.

Thanks,
Simon
Simon White
dCipher Computing
Post Reply