Android SQLite数据库不会更新和删除

时间:2016-06-25 19:22:40

标签: android sqlite sql-update sql-delete

这是提供用户输入的主要活动

public class Welcome extends AppCompatActivity{
private DBMANAGER_person dbmanager_person;
private ListView listView;
private SimpleCursorAdapter adapter;

final String [] from = new String[]{MyDB.COLUMN_ID, MyDB.COLUMN_NAME, MyDB.COLUMN_AGE, MyDB.COLUMN_HEIGHT , MyDB.COLUMN_WEIGHT};
final int [] to = new int[]{R.id.nameTV,R.id.ageTV,R.id.heightTV,R.id.weightTV};



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

    dbmanager_person = new DBMANAGER_person(this);
    dbmanager_person.open();

    Cursor cursor = dbmanager_person.fetch();

    listView = (ListView) findViewById(R.id.list_view);
    listView.setEmptyView(findViewById(R.id.emptyTV));

    adapter = new SimpleCursorAdapter(this, R.layout.person, cursor, from, to,0);
    adapter.notifyDataSetChanged();

    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            TextView idTextView = (TextView)view.findViewById(R.id.idTV);
            TextView nameTextView = (TextView) view.findViewById(R.id.nameTV);
            TextView ageTextView = (TextView) view.findViewById(R.id.ageTV);
            TextView heightTextView = (TextView) view.findViewById(R.id.heightTV);
            TextView weightTextView = (TextView) view.findViewById(R.id.weightTV);

            String iD = idTextView.getText().toString();
            String name = nameTextView.getText().toString();
            String age = ageTextView.getText().toString();
            String height = heightTextView.getText().toString();
            String weight = weightTextView.getText().toString();

            Intent intent = new Intent(getApplicationContext(), Modiffy_person_Details.class);
            intent.putExtra("_id",iD);
            intent.putExtra("name", name);
            intent.putExtra("age", age);
            intent.putExtra("height", height);
            intent.putExtra("weight", weight);

            startActivity(intent);


        }
    });
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.add_person,menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if(id == R.id.add){
        Intent intent = new Intent(this,Add_people.class);
        startActivity(intent);

    }else if (id == R.id.logout){
        Intent backToHome = new Intent(this,MainActivity.class);
        startActivity(backToHome);
    }

    return super.onOptionsItemSelected(item);
}

}

这是Add_prople类

public class Add_people extends AppCompatActivity实现了View.OnClickListener {     private EditText nameEditText;     private EditText ageEditText;     private EditText heightEditText;     private EditText weightEditText;

private Button save;
private DBMANAGER_person dbmanager_person;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setTitle("Add People");

    setContentView(R.layout.activity_add_people);

    nameEditText = (EditText)findViewById(R.id.nameET);
    ageEditText = (EditText)findViewById(R.id.ageET);
    heightEditText = (EditText)findViewById(R.id.heightET);
    weightEditText = (EditText)findViewById(R.id.weightET);

    save = (Button)findViewById(R.id.saveBtn);

    dbmanager_person = new DBMANAGER_person(this);
    dbmanager_person.open();
    save.setOnClickListener(this);
}


@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.saveBtn:
            final String name = nameEditText.getText().toString();
            final String age = ageEditText.getText().toString();
            final String height = heightEditText.getText().toString();
            final String weight = weightEditText.getText().toString();

            dbmanager_person.insert(name,age,height,weight);

            Intent main = new Intent(this,Welcome.class)
                    .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(main);
            break;

    }

}

}

这是CRUD操作的DBMANAGER_person类

这是Modiffy_person_Details类,用户可以在其中更新和删除他们的信息

public class DBMANAGER_person {
private SQLiteDatabase database;
private MyDB myDB;
private Context context;

public DBMANAGER_person(Context context) {
    this.context = context;
}

public DBMANAGER_person open() throws SQLiteException{
    myDB = new MyDB(context);
    database = myDB.getWritableDatabase();
    return this;
}

public void close(){
    myDB.close();
}
public void insert(String name,String age,String height,String weight){
    ContentValues contentValues = new ContentValues();
    contentValues.put(MyDB.COLUMN_NAME,name);
    contentValues.put(MyDB.COLUMN_AGE,age);
    contentValues.put(MyDB.COLUMN_HEIGHT,height);
    contentValues.put(MyDB.COLUMN_WEIGHT,weight);
    database.insert(MyDB.TABLE_NAME,null,contentValues);
}

public Cursor fetch(){
    String[] columns  = new String[]{MyDB.COLUMN_ID, MyDB.COLUMN_NAME, MyDB.COLUMN_AGE , MyDB.COLUMN_HEIGHT, MyDB.COLUMN_WEIGHT};
    Cursor cursor = database.rawQuery( "select rowid _id,* from "+MyDB.TABLE_NAME, null);
    cursor.moveToFirst();
    return cursor;
}

public int update(long id, String name, String age, String height, String weight){
    ContentValues contentValues = new ContentValues();
    contentValues.put(MyDB.COLUMN_NAME,name);
    contentValues.put(MyDB.COLUMN_AGE,age);
    contentValues.put(MyDB.COLUMN_HEIGHT,height);
    contentValues.put(MyDB.COLUMN_WEIGHT,weight);
    int i = database.update(MyDB.TABLE_NAME,contentValues,MyDB.COLUMN_ID + " = "+id,null);
    return i;
}

public void delete(long id){
    database.delete(MyDB.TABLE_NAME, MyDB.COLUMN_ID + "="+ id,null);
}

}

公共类Modiffy_person_Details扩展AppCompatActivity实现了View.OnClickListener {     private EditText nameField;     private EditText ageField;     private EditText heightField;     private EditText weightField;

private Button update;
private Button delete;

private long _id;
private DBMANAGER_person dbmanager_person;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitle("Modify Record");
    setContentView(R.layout.activity_modiffy_person__details);

    dbmanager_person = new DBMANAGER_person(this);
    dbmanager_person.open();

    nameField = (EditText)findViewById(R.id.nameEditText);
    ageField = (EditText)findViewById(R.id.ageEditText);
    heightField = (EditText)findViewById(R.id.heightEditText);
    weightField = (EditText)findViewById(R.id.weightEditText);

    update = (Button)findViewById(R.id.update_btn);
    delete = (Button)findViewById(R.id.delete_btn);

    Intent intent = getIntent();
    String ID = intent.getStringExtra("_id");
    String name = intent.getStringExtra("name");
    String age = intent.getStringExtra("age");
    String height = intent.getStringExtra("height");
    String weight = intent.getStringExtra("weight");

    String check_ID = ID;
    if(!check_ID.equals("")) {

        _id = Long.parseLong(ID);
    }/*else{
        Toast.makeText(getApplicationContext(),"There is no id",Toast.LENGTH_LONG).show();
    }*/



        nameField.setText(name);
        ageField.setText(age);
        heightField.setText(height);
        weightField.setText(weight);





    update.setOnClickListener(this);
    delete.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.update_btn:
            String na = nameField.getText().toString();
            String ag = ageField.getText().toString();
            String hei = heightField.getText().toString();
            String wei = weightField.getText().toString();

            dbmanager_person.update( _id , na , ag,hei,wei);
            this.returnHome();
            break;
        case R.id.delete_btn:
            dbmanager_person.delete(_id);
            this.returnHome();
            break;
    }

}

public void returnHome(){
    Intent home_intent = new Intent(getApplicationContext(),Welcome.class)
            .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(home_intent);
}

}

我的问题是,无论何时我想更新或删除我的信息,它都无效。

1 个答案:

答案 0 :(得分:0)

检查删除和更新的返回值。对于这两个函数,它在没有受影响的行时返回0,或者如果有行受影响则返回1。如果它是0,那么你的问题是处理SQL查询本身,在这种情况下,首先要检查的是ID的有效性。

update

delete