Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sum query in sqlite in android

I'm new to java,android and sqlite and i'm stuck at this. I have columns namely _id,type,amount(int),category,description.what i'm trying is to fetch the sum of amount of a particular category and store them in int. i know i need to provide the errors but it is simply giving me force close.Please Help

 public class TransactionReport extends Activity {
private static SQLiteDatabase db;
Cursor c;
int amount;
DbCrud localDbCrud = new DbCrud(this);

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.report);
    localDbCrud.open();                //localDatabase = localDbHelper.getWritableDatabase();  return this;
    c.moveToFirst();
    do{
        c = db.rawQuery("select sum(amount) from transaction_table where category = Salary ;", null);


      }while(c.moveToNext());

    amount = c.getInt(0);
    c.close();
    TextView ltxt = (TextView) findViewById(R.id.amt);
    ltxt.setText(""+amount);
    localDbCrud.close();  //localDatabase.close();


}

}

Would be helpful if explained in detail as i am a complete noob and i did try to find the solution but didnt find any.Thanks in advance

10-10 03:55:29.828: E/AndroidRuntime(10229): FATAL EXCEPTION: main
10-10 03:55:29.828: E/AndroidRuntime(10229): java.lang.RuntimeException: Unable to      start activity   ComponentInfo{com.hishighness.budgetracker/com.hishighness.budgetracker.TransactionReport}:     java.lang.NullPointerException
10-10 03:55:29.828: E/AndroidRuntime(10229):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
10-10 03:55:29.828: E/AndroidRuntime(10229):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
10-10 03:55:29.828: E/AndroidRuntime(10229):    at  android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-10 03:55:29.828: E/AndroidRuntime(10229):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
10-10 03:55:29.828: E/AndroidRuntime(10229):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-10 03:55:29.828: E/AndroidRuntime(10229):    at android.os.Looper.loop(Looper.java:130)
10-10 03:55:29.828: E/AndroidRuntime(10229):    at android.app.ActivityThread.main(ActivityThread.java:3687)
10-10 03:55:29.828: E/AndroidRuntime(10229):    at java.lang.reflect.Method.invokeNative(Native Method)
10-10 03:55:29.828: E/AndroidRuntime(10229):    at java.lang.reflect.Method.invoke(Method.java:507)
10-10 03:55:29.828: E/AndroidRuntime(10229):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
10-10 03:55:29.828: E/AndroidRuntime(10229):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
10-10 03:55:29.828: E/AndroidRuntime(10229):    at dalvik.system.NativeStart.main(Native Method)
10-10 03:55:29.828: E/AndroidRuntime(10229): Caused by: java.lang.NullPointerException
10-10 03:55:29.828: E/AndroidRuntime(10229):    at com.hishighness.budgetracker.TransactionReport.onCreate(TransactionReport.java:27)
10-10 03:55:29.828: E/AndroidRuntime(10229):    at  android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-10 03:55:29.828: E/AndroidRuntime(10229):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
10-10 03:55:29.828: E/AndroidRuntime(10229):    ... 11 more

Here's the log.

like image 918
Hishighness731 Avatar asked Oct 09 '12 22:10

Hishighness731


People also ask

How can I sum a column in SQLite in Android?

How can I sum a column in SQLite in Android? Syntax of SQLite SUM() Function Following is the syntax of the SQLite SUM() function to get the sum of values in a defined expression. Expression – Its column or expression which we used to calculate the sum of values in defined column or expression.

How to sum data in SQLite?

In SQLite SUM() Function is an aggregate function which is used to calculate the sum of values in a specified expression or column. Generally, in SQLite SUM() function will work with non-NULL numeric values to return the summed values of a column in a table.

How do I add two columns in SQLite?

SQLite does not support adding multiple columns to a table using a single statement. To add multiple columns to a table, you must execute multiple ALTER TABLE ADD COLUMN statements.

What is ContentValues Android SQLite?

ContentValues is a maplike class that matches a value to a String key. It contains multiple overloaded put methods that enforce type safety. Here is a list of the put methods supported by ContentValues: void put(String key, Byte value) void put(String key, Integer value)


1 Answers

Addition
I believe you want to use localDBCrud.rawQuery(), not db.rawQuery().

Original
I'm guessing that you have a NullPointerException. Here:

c.moveToFirst();

You are try to use c before you have initialized it with db.rawQuery().


You have some more logic errors here:

c.moveToFirst();
do{
    c = db.rawQuery("select sum(amount) from transaction_table where category = Salary ;", null);
  }while(c.moveToNext());

amount = c.getInt(0);
c.close();

Try this instead:

c = db.rawQuery("select sum(amount) from transaction_table where category = Salary ;", null);
if(c.moveToFirst())
    amount = c.getInt(0);
else
    amount = -1;
c.close();

Also @toadsky has made a great observation, follow his advice too.

like image 118
Sam Avatar answered Sep 28 '22 23:09

Sam