Python Forum

Full Version: Sqlite3 help with descending order
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
I am tring to use sqlite3 in python. So far I have got it working and can display values. However, when I try displaying it in descending order, it works with 1 digit numbers, but when a value has 2 digits, it will only count the first digit.
This is the code I use to display the values:
for row in c.execute('SELECT * FROM scores ORDER BY score1 DESC LIMIT 5'):
        print (row)
The output ends up being:
Output:
('Michael', '8') ('Jim', '2') ('Tim', '2') ('James', '2') ('Chris', '19')
However, I want it to put the highest numbers at the top and not just take the first digit into account.

Thanks.
Your "numbers" are stored as TEXT. So it is sorted as text. You either need to change the field type. Or if you HAVE TO store value as text (why?), cast it to INTEGER when query
(May-17-2019, 06:45 AM)buran Wrote: [ -> ]Your "numbers" are stored as TEXT. So it is sorted as text. You either need to change the field type. Or if you HAVE TO store value as text (why?), cast it to INTEGER when query


Thank you,

It worked when I created a new table and made it REAL instead of TEXT. However, now it prints it like:
('Jim', 19.0)
('Mark', 3.0)

Do you know how to get it to print without the decimal?
Thanks.
Make it INTEGER, not REAL
Thanks for all your help buran. This made it work. :)