Python Forum
Sqlite3 help with descending order
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sqlite3 help with descending order
#1
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.
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(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.
Reply
#4
Make it INTEGER, not REAL
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Thanks for all your help buran. This made it work. :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to sort values descending from a row in a dataframe using python sankarachari 1 1,395 Aug-16-2021, 08:55 AM
Last Post: jamesaarr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020