Reputation: 21
I have the following app that im trying to develop. Its a deposit, withdraw tracker that keeps track of an account. all the data is stored in a data base using mysql. I want to get a total of the column in my database but I keep getting an error when I click on the button to display the total
Here is my code
public class MainActivity extends Activity {
String total;
SQLiteDatabase db;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db=openOrCreateDatabase("MyDB1",MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS DepWith(total VARCHAR);");
}
public void data(View view)
{
EditText edittext1;
edittext1 = (EditText)findViewById(R.id.Deposit);
total=edittext1.getText().toString();
int total1 = Integer.parseInt(total);
db.execSQL("INSERT INTO DepWith VALUES('"+total+"');");
}
public void showdata(View view)
{
Cursor c=db.rawQuery("SELECT * from DepWith", null);
int count= c.getCount();
c.moveToFirst();
TableLayout tableLayout = new TableLayout(getApplicationContext());
tableLayout.setVerticalScrollBarEnabled(true);
TableRow tableRow;
TextView textView,textView1,textView2,textView3,textView4,textView5;
tableRow = new TableRow(getApplicationContext());
textView=new TextView(getApplicationContext());
textView.setText("Total");
textView.setTextColor(Color.RED);
textView.setTypeface(null, Typeface.BOLD);
textView.setPadding(20, 20, 20, 20);
tableRow.addView(textView);
tableLayout.addView(tableRow);
for (Integer j = 0; j < count; j++)
{
tableRow = new TableRow(getApplicationContext());
textView1 = new TextView(getApplicationContext());
textView1.setText(c.getString(c.getColumnIndex("total")));
textView1.setPadding(20, 20, 20, 20);
tableRow.addView(textView1);
tableLayout.addView(tableRow);
c.moveToNext() ;
}
setContentView(tableLayout);
db.close();
}
public void close(View view)
{
System.exit(0);
}
public void AddTotal(View view)
{
TextView tt;
tt=(TextView)findViewById(R.id.tt);
db=openOrCreateDatabase("MyDB1",MODE_PRIVATE, null);
Cursor resultSet = db.rawQuery("Select sum(total) from DepWith",null);
resultSet.moveToFirst();
String results = resultSet.getString(0);
tt.setText( results);
}
}
And here is the crash error I get
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3823)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3818)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: no such table: DepWith (code 1): , while compiling: Select sum(total) from DepWith
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
at com.example.app.MainActivity.AddTotal(MainActivity.java:86)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3818)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Upvotes: 0
Views: 610
Reputation: 57381
You create MyDB1
db=openOrCreateDatabase("MyDB1",MODE_PRIVATE, null);
But then try to open DepWith
which is a table
SQLiteDatabase db = openOrCreateDatabase("DepWith",MODE_PRIVATE,null);
Cursor resultSet = db.rawQuery("Select sum(total) from DepWith",null);
Upvotes: 3
Reputation: 152797
In your AddTotal()
you're opening a different database than in your onCreate()
:
SQLiteDatabase db = openOrCreateDatabase("DepWith",MODE_PRIVATE,null);
vs.
db=openOrCreateDatabase("MyDB1",MODE_PRIVATE, null);
That DepWith
database doesn't have the table.
You don't need to initialize another db
in your AddTotal
- just remove that line and use the db
initialized in onCreate()
.
Upvotes: 4