如何在数据库中更新类型为“ double”的列?

时间:2019-07-02 20:29:28

标签: java android sqlite android-studio

我一直在尝试更新其中存储数据类型为double的一列,但是它失败了,没有任何错误,我也不知道我做错了什么

我尝试同时使用rawquery和使用ContentValues进行更新,但都无法正常工作,我知道它与sqlite中的REAL数据类型有关,但我不知道如何使其正常工作

我的桌子

private static final String SQL_CREATE_TABLE_WOUND
            = "CREATE TABLE "
            + WOUND_TABLE_NAME + "("
            //+ WOUND_ID + " TEXT,"
            + WOUND_POSITION + " INT,"
            + WOUND_SIZE + " DOUBLE,"
            + WOUND_OWNER + " TEXT,"
            + WOUND_PHOTO + " TEXT,"
            + WOUND_THUMBNAIL + " TEXT,"
            + WOUND_TREATED + " INT DEFAULT 0,"
            + WOUND_NUMBER + " INT,"
            + WOUND_DATE + " INT,"
            +" FOREIGN KEY ("+WOUND_OWNER+") REFERENCES "+PATIENT_TABLE_NAME+"("+PATIENT_ID+"),"
            +" FOREIGN KEY ("+WOUND_POSITION+") REFERENCES "+WOUND_POSITION_TABLE_NAME+"("+POSITION_NUMBER+"),"
            +" PRIMARY KEY ("+ WOUND_OWNER +", "+ WOUND_POSITION +", "+ WOUND_NUMBER +", "+ WOUND_DATE +")"
            + ")"; 

这是我的更新功能

public boolean setWoundSizeValue(String ownerID,String position,String number,long date,Double value){
        boolean setSuccess = false;

        /*approach 2
        ContentValues cv = new ContentValues();
        cv.put(WOUND_SIZE,value);*/
            SQLiteDatabase db = this.getWritableDatabase();
            /*approach 1, didn't work
        try{
            String strSQL = "UPDATE "+WOUND_TABLE_NAME+" SET "+WOUND_SIZE+" = "+value+" WHERE "+WOUND_OWNER+" = "+ownerID+" AND "
                    + WOUND_POSITION+" = "+position+ " AND "+ WOUND_NUMBER +" = "+ number +" AND "+WOUND_DATE +" = "+date;
            Cursor c = db.rawQuery(strSQL,null);
            if(c!=null){
                if(c.moveToFirst()){
                    setSuccess = true;
                }
                c.close();
            }
        }catch (Exception e){
        }*/


           /*approach 2 didn't work
            long result = db.update(WOUND_TABLE_NAME,cv,WOUND_OWNER+"=? AND "+ WOUND_POSITION+"=? AND "+ WOUND_NUMBER +"=? AND "+WOUND_DATE+"=?"
            ,new String[]{ownerID,position,number,String.valueOf(date)});
            if(result>0){
                setSuccess = true;
            }*/

        return setSuccess;

    }

这是我调用更新功能的地方

public class SetWoundSizeActivity extends AppCompatActivity {

    private EditText setWoundSize;
    private Button saveButton;
    private String owner;
    private String position;
    private String woundNum;
    private String timer;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_wound_size);
        getSupportActionBar().setTitle("Set Wound Size");

        Intent intent = getIntent();

        owner = intent.getStringExtra("owner");
        position = intent.getStringExtra("position");
        woundNum = intent.getStringExtra("woundNum");
        timer = intent.getStringExtra("timer");

        setWoundSize = findViewById(R.id.wound_size);
        saveButton = findViewById(R.id.wound_size_button);

        saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DatabaseHelper helper = new DatabaseHelper(SetWoundSizeActivity.this);
                SQLiteDatabase db = helper.getWritableDatabase();
                final double woundSize = Double.valueOf(setWoundSize.getText().toString());
                Toast.makeText(SetWoundSizeActivity.this,"size is"+woundSize,Toast.LENGTH_LONG).show();
                //String ownerID,String position,String number,long date,float value
                boolean result;
                result = helper.setWoundSizeValue(owner,position,woundNum,Long.valueOf(timer),woundSize);
                if(result){
                    Toast.makeText(SetWoundSizeActivity.this,"size inserted",Toast.LENGTH_LONG).show();
                }else{Toast.makeText(SetWoundSizeActivity.this,"size not inserted",Toast.LENGTH_LONG).show();}
                finish();
            }
        });
    }
}

没有错误,但是在我所说的地方确认的结果始终是错误的

1 个答案:

答案 0 :(得分:0)

rawQuery()不能肯定起作用,因为它仅用于以Cursor的形式获取数据。
但是update()ContentValues确实可以使用,这是推荐的方式:

ContentValues cv = new ContentValues();
cv.put(WOUND_SIZE, value);
int rows = db.update(
  WOUND_TABLE_NAME, 
  cv,
  WOUND_OWNER + " = ? AND " + WOUND_POSITION + " = ? AND " + WOUND_NUMBER + " = ? AND " + WOUND_DATE + " = ?",
  new String[] {ownerID, position, number, String.valueOf(date)}
);

将在变量rows中返回更新的行数。