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
  sqlite3 question - execute method with :parameter richalt2 2 54 May-20-2019, 05:35 PM
Last Post: woooee
  sqlite3 window functions support using python2.7 Omkumar 2 82 May-03-2019, 11:57 AM
Last Post: Gribouillis
  how can a sqlite command like import be executed using sqlite3 (python) Larz60+ 1 112 Mar-08-2019, 01:17 AM
Last Post: Larz60+
  SQlite3 quickly increment INT value? jmair 1 99 Mar-04-2019, 08:03 PM
Last Post: stranac
  Python Turtle and order of implementation query Parsleigh 2 124 Mar-04-2019, 02:43 PM
Last Post: Parsleigh
  display the contents of a sqlite3 database according to the dates atlass218 4 167 Mar-03-2019, 06:43 AM
Last Post: atlass218
  Renaming explorer files in order? stroudie2 2 132 Mar-03-2019, 12:41 AM
Last Post: stroudie2
  Looking through files in explorer in order? stroudie2 0 126 Mar-02-2019, 11:06 AM
Last Post: stroudie2
  Python3 and sqlite3 using AND with SELECT darktitan 6 265 Mar-01-2019, 04:33 PM
Last Post: darktitan
  column order changes when copying rows in datafram swapnaharish 1 148 Feb-07-2019, 11:52 AM
Last Post: scidam

Forum Jump:


Users browsing this thread: 1 Guest(s)