Java SQLite通过单击“更新表中的某些列”;加上「;按钮错误和崩溃

Java SQLite通过单击“更新表中的某些列”;加上「;按钮错误和崩溃,java,android,sqlite,sql-update,android-sqlite,Java,Android,Sqlite,Sql Update,Android Sqlite,假设我的应用程序让用户输入金额,然后单击“添加”按钮将交易添加到“交易”表中,用户输入的金额也将更新“支出”表中指定类别(特定列/字段)的金额 添加按钮的代码如下所示: public void AddTransactions (final int temp_position) { button_add.setOnClickListener(new View.OnClickListener() { @Override public v

假设我的应用程序让用户输入金额,然后单击“添加”按钮将交易添加到“交易”表中,用户输入的金额也将更新“支出”表中指定类别(特定列/字段)的金额

添加按钮的代码如下所示:

public void AddTransactions (final int temp_position) {
        button_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                int temp_id = 0;

                Cursor transactionlatestid = myDb.getLatestTransactionID(temp_position);
                if (transactionlatestid.getCount() == 0) {
                    temp_id = 1;
                } else {
                    transactionlatestid.moveToFirst();
                    temp_id = transactionlatestid.getInt(0);
                    temp_id += 1;
                }

                String transaction_id = String.valueOf(temp_id);

                String flightposition_id = String.valueOf(temp_position);

                boolean isInserted = myDb.insertTransactionData(amount.getText().toString(), category, note.getText().toString(), transaction_id, flightposition_id);


                if (isInserted = true) {
                    Toast.makeText(AddExpenses.this, "Transaction Inserted !", Toast.LENGTH_LONG).show();
                    clearText();
                    temp += 1;
                    getLatestAmount(temp_position, category);
                }
                else {
                    Toast.makeText(AddExpenses.this, "Transaction not Inserted !", Toast.LENGTH_LONG).show();
                }


            }
        });
    }
public void getLatestAmount (int temp_position, String category) {
        temp_position += 1;
        String id = String.valueOf(temp_position);
        String temp_category = "";
        double amountfromdb = 5.00 ;

        if (category == "Food and Drinks")
        {
            temp_category = "foodndrinks";
        }
        else if (category == "Travel")
        {
            temp_category = "travel";
        }
        else if (category == "Shopping")
        {
            temp_category = "shopping";
        }
        else if (category == "Entertainment")
        {
            temp_category = "entertainment";
        }
        else if (category == "Others")
        {
            temp_category = "others";
        }

        Cursor latestamount = myDb.getLatestAmount(id, temp_category);
        if (latestamount.getCount() == 0) {
            Toast.makeText(AddExpenses.this, "No amount found !", Toast.LENGTH_LONG).show();
        } else {
            latestamount.moveToFirst();
            amountfromdb = latestamount.getDouble(0);
            updateAddTotalExpenses(id ,temp_category, amountfromdb);
        }
    }
成功添加事务时调用方法“getLatestAmount”。此方法用于获取“支出”表中当前已存在的指定类别的金额。例如,如果类别为“食品和饮料”,则此方法将从“spendings”表返回该列“FoodAndDrinks”中的值,然后调用“updateAddTotalExpenses”

代码如下:

public void AddTransactions (final int temp_position) {
        button_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                int temp_id = 0;

                Cursor transactionlatestid = myDb.getLatestTransactionID(temp_position);
                if (transactionlatestid.getCount() == 0) {
                    temp_id = 1;
                } else {
                    transactionlatestid.moveToFirst();
                    temp_id = transactionlatestid.getInt(0);
                    temp_id += 1;
                }

                String transaction_id = String.valueOf(temp_id);

                String flightposition_id = String.valueOf(temp_position);

                boolean isInserted = myDb.insertTransactionData(amount.getText().toString(), category, note.getText().toString(), transaction_id, flightposition_id);


                if (isInserted = true) {
                    Toast.makeText(AddExpenses.this, "Transaction Inserted !", Toast.LENGTH_LONG).show();
                    clearText();
                    temp += 1;
                    getLatestAmount(temp_position, category);
                }
                else {
                    Toast.makeText(AddExpenses.this, "Transaction not Inserted !", Toast.LENGTH_LONG).show();
                }


            }
        });
    }
public void getLatestAmount (int temp_position, String category) {
        temp_position += 1;
        String id = String.valueOf(temp_position);
        String temp_category = "";
        double amountfromdb = 5.00 ;

        if (category == "Food and Drinks")
        {
            temp_category = "foodndrinks";
        }
        else if (category == "Travel")
        {
            temp_category = "travel";
        }
        else if (category == "Shopping")
        {
            temp_category = "shopping";
        }
        else if (category == "Entertainment")
        {
            temp_category = "entertainment";
        }
        else if (category == "Others")
        {
            temp_category = "others";
        }

        Cursor latestamount = myDb.getLatestAmount(id, temp_category);
        if (latestamount.getCount() == 0) {
            Toast.makeText(AddExpenses.this, "No amount found !", Toast.LENGTH_LONG).show();
        } else {
            latestamount.moveToFirst();
            amountfromdb = latestamount.getDouble(0);
            updateAddTotalExpenses(id ,temp_category, amountfromdb);
        }
    }
此方法“updateAddTotalExpenses”用于将指定类别的“支出”表中的金额与用户输入的金额之和相加,然后将最新金额更新到“支出”表中指定的“类别”中

my DatabaseHelper.class中扩展SQLiteOpenHelper的“支出”表代码:-

    private static final String SPENDING_TABLE = "spendings";
    private static final String ID_SPENDING = "id";
    private static final String BUDGET = "budget";
    private static final String TOTAL_EXPENSES = "total_expenses";
    private static final String FOODNDRINKS = "foodndrinks";
    private static final String TRAVEL = "travel";
    private static final String SHOPPING = "shopping";
    private static final String ENTERTAINMENT = "entertainment";
    private static final String OTHERS = "others";
    private static final String BALANCE = "balance";

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + SPENDING_TABLE + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, BUDGET DOUBLE, TOTAL_EXPENSES DOUBLE, FOODNDRINKS DOUBLE, TRAVEL DOUBLE, SHOPPING DOUBLE, ENTERTAINMENT DOUBLE, OTHERS DOUBLE, BALANCE DOUBLE)");

        db.execSQL("CREATE TABLE " + TRANSACTION_TABLE + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, AMOUNT DOUBLE NOT NULL, CATEGORY TEXT NOT NULL, NOTE TEXT, TRANSACTION_ID TEXT NOT NULL, FLIGHT_ID INTEGER NOT NULL, FOREIGN KEY (FLIGHT_ID) REFERENCES FLIGHT_TABLE(id) )");
    }
下面的方法位于DatabaseHelper.class中,该类从主类调用,以获取“spendings”表中指定类别的数量

“updateFnDData”下面的方法位于DatabaseHelper.class中,该类从主类调用,以使用newestamount(旧金额+用户输入的新金额)更新指定类别的金额

***注意:现在我只有一个方法来更新“spendings”表中的一列,即“FoodDrinks”列,因为我只想在添加更多之前测试我的概念和代码是否正确

    public boolean updateFnDData(String id, String category, double newamount) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues contentValues = new ContentValues();

        db.update(SPENDING_TABLE, contentValues, "id = ?"+"category = ?", new String[] {id});

        return true;

    }
下面是当我在
编辑文本
框中输入金额后尝试单击“添加”按钮时出现的日志猫,交易已成功添加到“交易”表中,但指定类别的“支出”表中的金额未更新。特别是这两种方法
getLatestAmount
数据库助手.class
中的
“updateFnDData”

   --------- beginning of crash
10-23 22:07:23.419 4070-4070/project.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
    Process: project.myapplication, PID: 4070
    java.lang.NumberFormatException: empty String
        at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)
        at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
        at java.lang.Double.parseDouble(Double.java:539)
        at project.myapplication.AddExpenses.updateAddTotalExpenses(AddExpenses.java:231)
        at project.myapplication.AddExpenses.getLatestAmount(AddExpenses.java:225)
        at project.myapplication.AddExpenses$3.onClick(AddExpenses.java:155)
        at android.view.View.performClick(View.java:6294)
        at android.view.View$PerformClick.run(View.java:24770)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

updateFnDData()
的这些行中:

ContentValues contentValues = new ContentValues();
db.update(SPENDING_TABLE, contentValues, "id = ?"+"category = ?", new String[] {id});
将空的
contentValues
传递给
update()
方法

db.update()之前
设置
contentValues
如下:
contentValues.put(columnname,newamount)
columnname
替换为要更新的列的名称
编辑在需要更新的支出表中,没有
类别
列。也许您应该将
update()
方法的
WHERE
部分更改为类似于:
“id=?AND something=?”

Edit2
updateFnDData()
中对此进行更改:


添加您的建议!但我的应用程序仍然崩溃,logcat显示的“错误”代码与我在问题中发布的完全相同!:(参见我编辑过的答案。
“id=?”+“category=?”
“我明白你的意思!但是当我的“spendings”表中没有列“category”时,我之所以选择“category=?”是因为我希望这个更新方法能够更新“Food and Drinks”、“Travel”、“Shopping”和etc列,而不必为我要更新的每个列创建更新方法。“category”变量应该表示列名。因此,如果我将“category”变量中的“foodndweads”传递到update()方法中,我希望该方法更新“foodndweads”如果你明白我的意思,你知道我应该修改什么吗?我从那条语句中删除了+“category=?”,但仍然是相同的错误:(我发现了错误,因为“clearText()”在调用另一个方法之前!非常感谢!
ContentValues contentValues = new ContentValues();
contentValues.put("foodndrinks", newamount);
db.update(SPENDING_TABLE, contentValues, "id = ?", new String[] {id});