Bottom Page

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.
Quote
#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
Quote
#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.
Quote
#4
Make it INTEGER, not REAL
Quote
#5
Thanks for all your help buran. This made it work. :)
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Deleting data in sqlite3 JJ39 3 202 Jun-23-2019, 04:39 PM
Last Post: noisefloor
  Редактировать данные в sqlite3 JJ39 4 265 Jun-20-2019, 07:31 PM
Last Post: Larz60+
  maintaining list order 3Pinter 13 403 Jun-05-2019, 07:49 AM
Last Post: 3Pinter
  sqlite3 question - execute method with :parameter richalt2 2 202 May-20-2019, 05:35 PM
Last Post: woooee
  sqlite3 window functions support using python2.7 Omkumar 2 237 May-03-2019, 11:57 AM
Last Post: Gribouillis
  how can a sqlite command like import be executed using sqlite3 (python) Larz60+ 1 232 Mar-08-2019, 01:17 AM
Last Post: Larz60+
  SQlite3 quickly increment INT value? jmair 1 206 Mar-04-2019, 08:03 PM
Last Post: stranac
  Python Turtle and order of implementation query Parsleigh 2 267 Mar-04-2019, 02:43 PM
Last Post: Parsleigh
  display the contents of a sqlite3 database according to the dates atlass218 4 284 Mar-03-2019, 06:43 AM
Last Post: atlass218
  Renaming explorer files in order? stroudie2 2 248 Mar-03-2019, 12:41 AM
Last Post: stroudie2

Forum Jump:


Users browsing this thread: 1 Guest(s)