我有以下应用程序,我试图开发。它是一个存款,取款跟踪器,可以跟踪账户。所有数据都使用mysql存储在数据库中。我想在我的数据库中获得总计列,但是当我点击按钮显示总数时我一直收到错误
这是我的代码
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);
}
}
这是我得到的崩溃错误
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)
答案 0 :(得分:4)
在您的AddTotal()
中,您打开的数据库与onCreate()
中的数据库不同:
SQLiteDatabase db = openOrCreateDatabase("DepWith",MODE_PRIVATE,null);
VS
db=openOrCreateDatabase("MyDB1",MODE_PRIVATE, null);
DepWith
数据库没有该表。
您无需在db
中初始化其他AddTotal
- 只需删除该行,然后使用db
中初始化的onCreate()
。
答案 1 :(得分:3)
您创建MyDB1
db=openOrCreateDatabase("MyDB1",MODE_PRIVATE, null);
然后尝试打开DepWith
这是一个表
SQLiteDatabase db = openOrCreateDatabase("DepWith",MODE_PRIVATE,null);
Cursor resultSet = db.rawQuery("Select sum(total) from DepWith",null);