更新数据库中的列不起作用

时间:2018-01-08 12:57:03

标签: java sql database sqlite sqliteopenhelper

我有一个名字列,另一个有分数。我每次按下某个按钮时都会尝试更新数据库中的分数,这个按钮会为数据库添加一定数量的分数,但它无法正常工作。我如何从数据库中正确获取数据并进行更新?这与数据库版本有关吗?

谢谢。

数据库类

public class myDbAdapter extends SQLiteOpenHelper{


public static final String DATABASE_NAME = "Castaways.db";
public static final String TABLE_NAME = "Survivor_Table";
public static final String COL_1 = "ID";
public static final String COL_2 = "NAME";
public static final String COL_3 = "MARK";

public myDbAdapter(Context context) {
    super(context, DATABASE_NAME, null, 3);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,MARK INTEGER)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
    onCreate(db);
}

public boolean addData(String name,int score) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_2,name);
    contentValues.put(COL_3,score);
    long result = db.insert(TABLE_NAME,null ,contentValues);
    if(result == -1)
        return false;
    else
        return true;
}

public Cursor getData() {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("select * from "+TABLE_NAME,null);
    return res;
}

public Cursor getItemID(String name){
    SQLiteDatabase db = this.getWritableDatabase();
    String query = "SELECT " + COL_1 + " FROM " + TABLE_NAME +
            " WHERE " + COL_2 + " = '" + name + "'";
    Cursor data = db.rawQuery(query, null);
    return data;
}

public Cursor getScore(int id){
    SQLiteDatabase db = this.getWritableDatabase();
    String query = "SELECT " + COL_3 + " FROM " + TABLE_NAME +
            " WHERE " + COL_1 + " = '" + id + "'";
    Cursor data = db.rawQuery(query, null);
    return data;
}

public void updateName(String newName, int id, String oldName){
    SQLiteDatabase db = this.getWritableDatabase();
    String query = "UPDATE " + TABLE_NAME + " SET " + COL_2 +
            " = '" + newName + "' WHERE " + COL_1 + " = '" + id + "'" +
            " AND " + COL_2 + " = '" + oldName + "'";
    Log.d(TAG, "updateName: query: " + query);
    Log.d(TAG, "updateName: Setting name to " + newName);
    db.execSQL(query);
}


public Integer deleteData(String id) {
    SQLiteDatabase db = this.getWritableDatabase();
    return db.delete(TABLE_NAME, "ID = ?",new String[] {id});
}

public void deleteName(int id, String name){
    SQLiteDatabase db = this.getWritableDatabase();
    String query = "DELETE FROM " + TABLE_NAME + " WHERE "
            + COL_1 + " = '" + id + "'" +
            " AND " + COL_2 + " = '" + name + "'";
    Log.d(TAG, "deleteName: query: " + query);
    Log.d(TAG, "deleteName: Deleting " + name + " from database.");
    db.execSQL(query);
}

public void updateScore(int newScore, int id){
    SQLiteDatabase db = this.getWritableDatabase();
    String query = "UPDATE " + TABLE_NAME + " SET " + COL_3 +
            " = '" + newScore + "' WHERE " + COL_1 + " = '" + id + "'";
    Log.d(TAG, "updateScore: query: " + query);
    Log.d(TAG, "updateScore: Setting score to " + newScore);
    db.execSQL(query);
}

public void updateScore2(int newScore, int id, int oldScore){
    SQLiteDatabase db = this.getWritableDatabase();
    String query = "UPDATE " + TABLE_NAME + " SET " + COL_3 +
            " = '" + newScore + "' WHERE " + COL_1 + " = '" + id + "'" +
            " AND " + COL_3 + " = '" + oldScore + "'";
    Log.d(TAG, "updateScore: query: " + query);
    Log.d(TAG, "updateScore: Setting score to " + newScore);
    db.execSQL(query);
}

public void clearData()
{
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_NAME,null,null);
    db.close();
}

}

添加积分等级

public class Outcomes extends Activity {


private TextView castname;
myDbAdapter mydb;
private String selectedCastaway;
private int selectedID;
private int points = 0;
private int castscore;
EditCastaway EC = new EditCastaway();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_outcomes);

    mydb = new myDbAdapter(this);

    //get the intent extra from the ListDataActivity
    Intent receivedIntent = getIntent();

    //now get the itemID we passed as an extra
    selectedID = receivedIntent.getIntExtra("id",-1); //NOTE: -1 is just the default value

    //now get the name we passed as an extra
    selectedCastaway = receivedIntent.getStringExtra("name");

    defineButtons();

    castname.setText(selectedCastaway);

    castscore = receivedIntent.getIntExtra("score", 0);



}




public void defineButtons()
{
    findViewById(R.id.out1).setOnClickListener(buttonClickListener);
    findViewById(R.id.out2).setOnClickListener(buttonClickListener);
    findViewById(R.id.out3).setOnClickListener(buttonClickListener);
    findViewById(R.id.out4).setOnClickListener(buttonClickListener);
    findViewById(R.id.out5).setOnClickListener(buttonClickListener);
    findViewById(R.id.out6).setOnClickListener(buttonClickListener);
    findViewById(R.id.out7).setOnClickListener(buttonClickListener);
    findViewById(R.id.out8).setOnClickListener(buttonClickListener);
    findViewById(R.id.out9).setOnClickListener(buttonClickListener);
    findViewById(R.id.out10).setOnClickListener(buttonClickListener);
    findViewById(R.id.out11).setOnClickListener(buttonClickListener);
    findViewById(R.id.out12).setOnClickListener(buttonClickListener);
    findViewById(R.id.out13).setOnClickListener(buttonClickListener);
    findViewById(R.id.out14).setOnClickListener(buttonClickListener);
    findViewById(R.id.out15).setOnClickListener(buttonClickListener);
    findViewById(R.id.out16).setOnClickListener(buttonClickListener);
    findViewById(R.id.out17).setOnClickListener(buttonClickListener);
    findViewById(R.id.out18).setOnClickListener(buttonClickListener);
    findViewById(R.id.out19).setOnClickListener(buttonClickListener);
    findViewById(R.id.out20).setOnClickListener(buttonClickListener);
    findViewById(R.id.out21).setOnClickListener(buttonClickListener);

    castname = (TextView) findViewById(R.id.castname);
}



private View.OnClickListener buttonClickListener = new View.OnClickListener() {






    public void onClick(View v) {


        switch (v.getId()) {


            case R.id.out1:
                points = 15;
                toastAdd(points);
                /*Cursor data = mydb.getScore(selectedID);
                while(data.moveToNext())
                {
                    castscore = data.getInt(0);
                }*/

                mydb.updateScore2(castscore + points, selectedID, castscore);
                castscore = castscore + points;


                break;


            case R.id.out2:
                points = 20;
                toastAdd(points);
                mydb.updateScore2(castscore + points,selectedID,castscore);
                castscore = castscore + points;
                break;

            case R.id.out3:
                points = 5;
                toastAdd(points);
                mydb.updateScore2(castscore + points,selectedID,castscore);
                castscore = castscore + points;

                break;

            case R.id.out4:
                points = 30;
                toastAdd(points);
                mydb.updateScore2(castscore + points,selectedID,castscore);
                castscore = castscore + points;

                break;

            case R.id.out5:
                points = 10;
                toastAdd(points);
                mydb.updateScore2(castscore + points,selectedID,castscore);
                castscore = castscore + points;

                break;

            case R.id.out6:
                points = 5;
                toastAdd(points);
                mydb.updateScore2(castscore + points,selectedID,castscore);
                castscore = castscore + points;
                break;

            case R.id.out7:
                points = 10;
                toastAdd(points);
                mydb.updateScore2(castscore + points,selectedID,castscore);
                castscore = castscore + points;

                break;

            case R.id.out8:
                points = 10;
                toastAdd(points);
                mydb.updateScore2(castscore + points,selectedID,castscore);
                castscore = castscore + points;

                break;

            case R.id.out9:
                points = 15;
                toastAdd(points);
                mydb.updateScore2(castscore + points,selectedID,castscore);
                castscore = castscore + points;

                break;

            case R.id.out10:
                points = 10;
                toastAdd(points);
                mydb.updateScore2(castscore + points,selectedID,castscore);
                castscore = castscore + points;

                break;

            case R.id.out11:
                points = 10;
                toastAdd(points);
                mydb.updateScore2(castscore + points,selectedID,castscore);
                castscore = castscore + points;

                break;

            case R.id.out12:
                points = 15;
                toastAdd(points);
                mydb.updateScore2(castscore + points,selectedID,castscore);
                castscore = castscore + points;

                break;

            case R.id.out13:
                points = -5;
                toastRemove(points);
                mydb.updateScore2(castscore + points,selectedID,castscore);
                castscore = castscore + points;

                break;

            case R.id.out14:
                toastRemove(points);
                mydb.updateScore2(castscore + points,selectedID,castscore);
                castscore = castscore + points;

                break;

            case R.id.out15:
                points = -40;
                toastRemoveEliminate(points);
                mydb.updateScore2(castscore + points,selectedID,castscore);
                castscore = castscore + points;

                if(EC.radbut.isChecked())
                {
                    EC.radbut.toggle();
                }



                break;

            case R.id.out16:
                points = -15;
                toastRemove(points);
                mydb.updateScore2(castscore + points,selectedID,castscore);
                castscore = castscore + points;

                break;

            case R.id.out17:
                points = -2;
                toastRemove(points);
                mydb.updateScore2(castscore + points,selectedID,castscore);
                castscore = castscore + points;

                break;

            case R.id.out18:
                points = -7;
                toastRemoveEliminate(points);
                mydb.updateScore2(castscore + points,selectedID,castscore);
                castscore = castscore + points;

                break;

            case R.id.out19:
                points = 60;
                toastRemove(points);
                mydb.updateScore2(castscore + points,selectedID,castscore);
                castscore = castscore + points;


                break;

            case R.id.out20:
                points = 30;
                toastRemove(points);
                mydb.updateScore2(castscore + points,selectedID,castscore);
                castscore = castscore + points;

                break;

            case R.id.out21:
                points = 10;
                toastRemove(points);
                mydb.updateScore2(castscore + points,selectedID,castscore);
                castscore = castscore + points;

                break;




        }
    }
};



private void toastText(String text)
{
    Toast.makeText(this,text, Toast.LENGTH_SHORT).show();
}

private void toastAdd(int points)
{
    Toast toast=Toast.makeText(this,points + " points has been added to " + selectedCastaway,Toast.LENGTH_SHORT);
    toast.show();
}

private void toastRemove(int points)
{
    points = points*-1;
    Toast toast=Toast.makeText(this,points + " points has been removed from " + selectedCastaway,Toast.LENGTH_SHORT);
    toast.show();
}

private void toastRemoveEliminate(int points)
{
    points = points*-1;
    Toast toast=Toast.makeText(this,points + " points has been removed from " + selectedCastaway + " This Castaway is now no longer in the game!",Toast.LENGTH_SHORT);
    toast.show();
}
}

1 个答案:

答案 0 :(得分:0)

在updateName中:

WHERE " + COL_1 + " = '" + id + "'"

id是一个int所以引用是错误的。 应该是:

WHERE " + COL_1 + " = " + id

但是,你的代码是主题代码SQL注入;使用PreparedStatement提高性能和安全性