开发者

Group by query is not working in android 2.1

开发者 https://www.devze.com 2023-04-04 21:20 出处:网络
I am saving datetime in milliseconds in my local database table \"session\" My problem is this code is working in android 2.2 and above but belove version of android 2.2

I am saving datetime in milliseconds in my local database table "session" My problem is this code is working in android 2.2 and above but belove version of android 2.2 it is not working means it is giving me all the rows from table But is I run same code in android 2.2 it is working perfactly and giving me distinct values

When I run below query in Sqlite Manager it is giving me proper distinct value

Create table syntax

"CREATE TABLE  session  ( " +
        "recid  INTEGER PRIMARY KEY NOT NULL ,  " +
        "starttime  TEXT)";

Query to fetch data from database

Cursor c = mDb.rawQuery("select distinct (starttime / (24 * 60 * 60 * 1000)) ,
starttime from session group by (starttime / (24 * 60 * 60 * 1000))", null);

Values in database table session ::

1   1319084700198
4   1319084100642
5   1319092500940
6   1319092500769
7   1319100300864
8   1319107500002
9   1319107500673
10  1319168700236
11  1319169000223
12  1319178000717
13  1319178000902
14  1319185800738
15  13191921开发者_如何学Go00137
16  1319256000842
17  1319256000985
18  1319192100156
19  1319084700210
20  1319085000660
21  1319086500972
22  1319088000927
23  1319092500687
24  1319092500027
25  1319092500178
26  1319092500702
27  1319092500878
28  1319092500114


I think the root to you problems lies in mixing strings and arithmetic's in the SQL. Consider changing the database type from TEXT to INTEGER.


If you must use text for some reason, I would try calculating the values outside the SQL and provide the calculated values instead. This way you could catch NumberFormatException and the like.

distinctTime = Double.parseDouble(starttime) / (24 * 60 * 60 * 1000);
Cursor c = mDb.rawQuery("select distinct " + distinctTime, 
"starttime from session group by " +distinctTime, null);
0

精彩评论

暂无评论...
验证码 换一张
取 消