SQLite question/problem..

Put your problem here if it does not fit any of the other categories.

SQLite question/problem..

Postby jonaaathan » Sat Jul 25, 2009 10:18 am

Please help...

I have a column that contains a list of numeric values. Is there any way so that I can add all those values up and display the total in a TextView, and how would I do that?

Thanks!
jonaaathan
Freshman
Freshman
 
Posts: 5
Joined: Wed Jun 17, 2009 4:46 am

Top

Postby nmc » Sat Jul 25, 2009 10:28 am

Do a rawQuery() using:
select sum(myColumn) from myTable
nmc
Senior Developer
Senior Developer
 
Posts: 154
Joined: Thu Nov 27, 2008 8:30 pm
Location: Germany

Postby jonaaathan » Sat Jul 25, 2009 11:26 am

nmc wrote:Do a rawQuery() using:
select sum(myColumn) from myTable


Thanks for the reply, but I'm not familiar with SQLite.. I did some research and I ended up coming up with this code..

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 public void getTotal() {
  2.                 sqliteDatabase2.rawQuery(
  3.                 "SELECT SUM(COL_ITEM2) as sum FROM Table", null );
  4.         }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


is that correct?
jonaaathan
Freshman
Freshman
 
Posts: 5
Joined: Wed Jun 17, 2009 4:46 am

Postby nmc » Sat Jul 25, 2009 12:05 pm

Well, you have to fetch the result (its not in your code), but the query will do the trick, yes.

...are you sure you have named your table "Table"?
You shouldnt do that since its a reserved token :?
nmc
Senior Developer
Senior Developer
 
Posts: 154
Joined: Thu Nov 27, 2008 8:30 pm
Location: Germany

Postby jonaaathan » Sat Jul 25, 2009 9:02 pm

nmc wrote:Well, you have to fetch the result (its not in your code), but the query will do the trick, yes.

...are you sure you have named your table "Table"?
You shouldnt do that since its a reserved token :?


the table name is actually dbTable. i figured that i needed to do something else because when calling getTotatl(); catlog shows that no column name was found.. again im not quite sure on how to fetch the results.
jonaaathan
Freshman
Freshman
 
Posts: 5
Joined: Wed Jun 17, 2009 4:46 am

Postby nmc » Sun Jul 26, 2009 12:15 am

the table name is actually dbTable. i figured that i needed to do something else because when calling getTotatl(); catlog shows that no column name was found..


...but you got this running now?


again im not quite sure on how to fetch the results


How do you get results when fetching direct data, e.g. for displaying database contents?
Use cursor.getInt(0), since the column will not match any column-name used in your table, but will something like "SUM(COL_ITEM2)".

If your query is "SELECT SUM(COL_ITEM2) as sum FROM dbTable", then you can fetch the result using "sum" as findColumn() Parameter (not sure if the function name really is findColumn, but you get the idea)
nmc
Senior Developer
Senior Developer
 
Posts: 154
Joined: Thu Nov 27, 2008 8:30 pm
Location: Germany

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 21 guests