如何在我的Sqlite数据库中添加删除功能?

时间:2016-02-20 16:13:58

标签: java android sqlite android-sqlite

我正在构建一个使用sqlite数据库保存用户输入内容的培训日志。目前,我只有一个附加到数据库的add方法,但不确定如何创建删除创建的行的delete方法。用户数据最初是在TrainingLogCreate(class)中创建和添加的,它访问DBAdapter的(Class)add方法。我可以在DBAdapter中添加哪些删除方法以删除用户条目?

TrainingLogCreate:

public class TrainingLogCreate extends AppCompatActivity {


EditText nameTxt;
EditText posTxt;
Button savebtn;
Context context = this;

public TrainingLogCreate() {
    // Required empty public constructor
}



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

    setContentView(R.layout.training_log_create);

    savebtn = (Button) findViewById(R.id.saveButton);
    nameTxt = (EditText) findViewById(R.id.exercizeActivity);
    posTxt = (EditText) findViewById(R.id.exercizeDetails);
    final DBAdapter db = new DBAdapter(this);

    SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
    String datetime;
    try {
        Date date = new Date();
        datetime = dateFormat.format(date);


    } finally {

    }

    final String dateTxt = datetime;

    savebtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            // TODO Auto-generated method stub


            //OPEN
            db.openDB();

            //INSERT
            long result = db.add(nameTxt.getText().toString(), posTxt.getText().toString(), dateTxt.toString());

            if (result > 0) {
                nameTxt.setText("");
                posTxt.setText("");

            } else {
                Toast.makeText(getApplicationContext(), "Failure", Toast.LENGTH_SHORT).show();
            }


            //CLOSE DB
            db.close();

            //Open Fragment


            finish();

        }
    });


}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater mif = getMenuInflater();
    mif.inflate(R.menu.training_create_menu, menu);
    getActionBar().show();
    return super.onCreateOptionsMenu(menu);




}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // handle item selection
    switch (item.getItemId()) {
        case R.id.action_save:
            //add save functionality
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}


}

将对DBAdapter:

    public class DBAdapter {

    //COLUMNS
    static final String ROWID="id";
    static final String NAME = "name";
    static final String POSITION = "position";
    static final String DATE = "date";

    //DB PROPERTIES
    static final String DBNAME="m_DB";
    static final String TBNAME="m_TB";
    static final int DBVERSION='1';

    static final String CREATE_TB="CREATE TABLE m_TB(id INTEGER PRIMARY KEY AUTOINCREMENT,"
            + "name TEXT NOT NULL,position TEXT NOT NULL,date TEXT NOT NULL);";

    final Context c;
    SQLiteDatabase db;
    DBHelper helper;

    public DBAdapter(FragmentActivity ctx) {
        // TODO Auto-generated constructor stub

        this.c=ctx;
        helper=new DBHelper(c);
    }

    // INNER HELPER DB CLASS
    private static class DBHelper extends SQLiteOpenHelper
    {

        public DBHelper(Context context ) {
            super(context, DBNAME, null, DBVERSION);
            // TODO Auto-generated constructor stub
        }

        @Override
        public void onCreate(SQLiteDatabase db) {

            try
            {
                db.execSQL(CREATE_TB);
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub

            Log.w("DBAdapetr","Upgrading DB");

            db.execSQL("DROP TABLE IF EXISTS m_TB");

            onCreate(db);
        }

    }

    // OPEN THE DB
    public DBAdapter openDB()
    {
        try
        {
            db=helper.getWritableDatabase();

        }catch(SQLException e)
        {
            Toast.makeText(c, e.getMessage(), Toast.LENGTH_LONG).show();
        }

        return this;
    }


    //CLOSE THE DB
    public void close()
    {
        helper.close();
    }

    //INSERT INTO TABLE
    public long add(String name,String pos, String date)
    {
        try
        {
            ContentValues cv=new ContentValues();
            cv.put(NAME, name);
            cv.put(POSITION, pos);
            cv.put(DATE, date);

            return db.insert(TBNAME, ROWID, cv);

        }catch(SQLException e)
        {
            e.printStackTrace();
        }

        return 0;
    }

    //REMOVE FROM TABLE
    public long remove(String name)
    {




    }


    //GET ALL VALUES

    public Cursor getAllNames()
    {
        String[] columns={ROWID,NAME,POSITION,DATE};

        return db.query(TBNAME, columns, null, null, null, null, null);
    }





}

1 个答案:

答案 0 :(得分:1)

您可以使用这样的删除方法

public void deleteInterestId(int id) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_TIMEKEEPER, KEY_ID + "=?", new String[]{String.valueOf(id)});
    db.close();
}

您必须传递要删除的ID。您必须稍微自定义此方法,但它会给您一个想法

删除最重要的是你必须创建一个名为id的列,并在插入记录时增加它的值。因为您无法在数据库

自动创建的id的基础上删除