

Toast.makeText(this, "Before setAdapter", Toast.LENGTH_SHORT). I have had similar problem because I was not adding the _id column to the projection argument, so adding _id to the projections argument of the query was the solution. Solution: add a space between the left parenthesis '(' and _id Solution 2
#Android sqlite order by numbers update
UPDATE "CREATE TABLE IF NOT EXISTS contact_data( _id INTEGER PRIMARY KEY AUTOINCREMENT, contact_id INTEGER, contact_name VARCHAR(50), number_type VARCHAR(50), contact_number VARCHAR(50), duration TIME, duration_sum TIME, date DATE, current_time TIME, cont INTEGER, type VARCHAR, month VARCHAR(50), day VARCHAR(50), year VARCHAR(50)) " I believe this is a requirement that is required in order to use a SimpleCursorAdapter. Its declartion looks something like this: _id INTEGER PRIMARY KEY AUTOINCREMENTĪdd this and you will then be able to use it. Its as simple as editing your table creation statement and adding a column called _id. This way, youll sort the data in ascending order by this column. > timeit.Timer(cast_affinity, setup).timeit(number = 1)Īs we can see its a bit slower though not by much, interesting.You are trying to use a cursor that REQUIRES a column called _id. Discussion: Use the ORDER BY keyword and the name of the column by which you want to sort. > timeit.Timer(cast_conv, setup).timeit(number = 1) > cast_affinity = "result = c.execute('SELECT * FROM T ORDER BY (value + 0)')" > cast_conv = "result = c.execute('SELECT * FROM T ORDER BY CAST(value AS INTEGER)')" _ = c.execute('INSERT INTO T (value) VALUES (%i)' % index) I was curios so I ran some benchmarks: > setup = """ An operand on a mathematical operator that does not look in any way numeric and is not NULL is converted to 0 or 0.0. A NULL operand on a mathematical operator yields a NULL result.

The cast is carried through even if it is lossy and irreversible. Application of a CAST expression is different to application of a column affinity, as with a CAST expression the storage class conversion is forced even if it is lossy and irrreversible.Īll mathematical operators (+, -, *, /, %, >, &, and |) cast both operands to the NUMERIC storage class prior to being carried out. Well thats interesting, though I dont know how many DBMS support such an operation so I don't recommend it just in case you ever need to use a different system that doesn't support it, not to mention you are adding an extra operation, which can affect performance, though you also do this ORDER BY (field + 0) Im going to investigate the performanceĪ CAST expression is used to convert the value of to a different storage class in a similar way to the conversion that takes place when a column affinity is applied to a value. Is the a way to "officially" convert it like C's atoi? If I do this: ".ORDER BY (field+1)" I can workaround this, because somehow the string is internally being converted to integer. Sqlite> SELECT * FROM T ORDER BY CAST(value AS INTEGER) Sqlite> INSERT INTO T (value) VALUES ('03') Sqlite> INSERT INTO T (value) VALUES ('02') Sqlite> INSERT INTO T (value) VALUES ('01') Sqlite> INSERT INTO T (value) VALUES ('12') Sqlite> INSERT INTO T (value) VALUES ('11') Sqlite> INSERT INTO T (value) VALUES ('10') sqlite> CREATE TABLE T (value VARCHAR(2))

You can use CAST to cast the expression to an Integer.
