单击特定条目后删除sqlite数据库中的条目

时间:2014-09-01 11:39:08

标签: android sqlite

我已经制作了一个基本的应用程序,其中我使用sqlite数据库来管理来宾的记录,我也使用删除功能删除条目,但使用他们的ID,所以,我想在点击后执行此操作特别是我要删除它的条目。我是android的新手,所以请帮助我,我已经尝试了很多像我的代码如下来的访客列表和plz告诉我添加到哪里使这种情况发生:这是我的数据库类

public class HotOrNot
{
    public static final String  KEY_ROWID           = "_id";
    public static final String  KEY_NAME            = "guest_name";
    public static final String  KEY_HOTNESS         = "guest_address";

    private static final String DATABASE_NAME       = "Hotdbdb";
    private static final String DATABASE_TABLE      = "peopleTable";
    private static final int    DATABASE_VERSION    = 1;

    private DbHelper            ourHelper;
    private final Context       ourContext;
    private SQLiteDatabase      ourDatabase;

    private static class DbHelper extends SQLiteOpenHelper
    {

        public DbHelper(Context context)
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
       }

    @Override
    public void onCreate(SQLiteDatabase db)
    {
        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID + " INTEGER PRIMARY KEY , " + KEY_NAME + " TEXT NOT NULL, " + KEY_HOTNESS + " TEXT NOT NULL);");
    }

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

public HotOrNot(Context c)
{
    ourContext = c;
}

public HotOrNot open() throws SQLException
{
    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;
}

public void close()
{
    ourHelper.close();
}


public long createEntry(String name, String hotness)
{
    ContentValues cv = new ContentValues();
    cv.put(KEY_NAME, name);
    cv.put(KEY_HOTNESS, hotness);
    return ourDatabase.insert(DATABASE_TABLE, null, cv);
}

public String getData()
{
    String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_HOTNESS};
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
    String result = "";

    int iRow = c.getColumnIndex(KEY_ROWID);
    int iName = c.getColumnIndex(KEY_NAME);
    int iHotness = c.getColumnIndex(KEY_HOTNESS);

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
    {
        result = result + c.getString(iRow) + "     " + c.getString(iName) + "               " + c.getString(iHotness) + "\n";
    }

    return result;

}



public String getName(long l) {
    // TODO Auto-generated method stub
    String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_HOTNESS};
    Cursor c= ourDatabase.query(DATABASE_TABLE,columns,KEY_ROWID + "=" + l, null ,null,null,null);
    if(c != null){
        c.moveToFirst();
        String name=c.getString(1);
        return name;
    }
    return null;
}

public String getHotness(long l) {
    // TODO Auto-generated method stub
    String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_HOTNESS};
    Cursor c= ourDatabase.query(DATABASE_TABLE, columns,  KEY_ROWID + "=" + l, null ,null,null,null);
    if(c != null){
        c.moveToFirst();
        String hotness=c.getString(2);
        return hotness;
    }
    return null;
}

public void updateEntry(long lRow, String mName, String mHotness) throws SQLException {
    // TODO Auto-generated method stub
    ContentValues cvUpdate=new ContentValues();
    cvUpdate.put(KEY_NAME,mName);
    cvUpdate.put(KEY_HOTNESS, mHotness);
    ourDatabase.update(DATABASE_TABLE,cvUpdate,KEY_ROWID + "=" + lRow,null);




}

public void DeletEntry(long lRow1)  throws SQLException {
    // TODO Auto-generated method stub
    ourDatabase.delete(DATABASE_TABLE, KEY_ROWID + "=" + lRow1, null);

}



private ListView findViewById(Object list) {
    // TODO Auto-generated method stub
    return null;
}

    }

这是我的添加访客班级

public class add_Guest extends Activity implements OnClickListener
{
    Button  sqlUpdate, sqlView,sqlDelete,sqlGetInfo,sqlModify;
    EditText    sqlName, sqlHotness,sqlRow;
    private Context context;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_guest);
    sqlUpdate = (Button) findViewById(R.id.bSQLUpdate);
    sqlName = (EditText) findViewById(R.id.etSQLName);
    sqlHotness = (EditText) findViewById(R.id.etSQLHotness);
    sqlView = (Button) findViewById(R.id.bSQLopenView);
    Button sqlex = (Button) findViewById(R.id.bexport);
    sqlView.setOnClickListener(this);
    sqlUpdate.setOnClickListener(this);
    sqlRow = (EditText) findViewById(R.id.etSQLrowinfo);
    sqlDelete = (Button) findViewById(R.id.bSQLdelete);
    sqlGetInfo= (Button) findViewById(R.id.bgetinfo);
    sqlModify = (Button) findViewById(R.id.bSQLmodify);
    sqlDelete.setOnClickListener(this);
    sqlModify.setOnClickListener(this);
    sqlGetInfo.setOnClickListener(this);
    sqlex.setOnClickListener(this);
}

public void onClick(View arg0){
    int id = arg0.getId();
    if (id == R.id.bSQLUpdate) {
        boolean didItWork = true;
        try
        {
            String name = sqlName.getText().toString();
            String hotness = sqlHotness.getText().toString();

            HotOrNot entry = new HotOrNot(add_Guest.this);
            entry.open();
            entry.createEntry(name, hotness);
            entry.close();
        }
        catch(Exception e )
        {
            didItWork = false;
            String error = e.toString();
            Dialog d = new Dialog(this);
            d.setTitle("CATCH");
            TextView tv = new TextView(this);
            tv.setText(error);
            d.setContentView(tv);
            d.show();
        }

        finally
        {
            if (didItWork)
            {
                Dialog d = new Dialog(this);
                d.setTitle("FINALLY");
                TextView tv = new TextView(this);
                tv.setText("GOOD");
                d.setContentView(tv);
                d.show();
            }
        }
    } else if (id == R.id.bSQLopenView) {
        Intent i = new Intent("com.example.datewithme.SQLVIEW");
        startActivity(i);
    } else if (id == R.id.bgetinfo) {
         try{
        String s = sqlRow.getText().toString();
        long l= Long.parseLong(s);
        HotOrNot hon=new HotOrNot(this);
        hon.open();
        String returnedName=hon.getName(l);
        String returnedHotness=hon.getHotness(l);
        hon.close();
        sqlName.setText(returnedName);
        sqlHotness.setText(returnedHotness);
     }
    catch(Exception e )
    {

        String error = e.toString();
        Dialog d = new Dialog(this);
        d.setTitle("CATCH");
        TextView tv = new TextView(this);
        tv.setText(error);
        d.setContentView(tv);
        d.show();
    }
    } 
else if (id == R.id.bSQLmodify) {
         try{String mName = sqlName.getText().toString();
         String mHotness = sqlHotness.getText().toString();
         String sRow = sqlRow.getText().toString();
         long lRow= Long.parseLong(sRow);
         HotOrNot ex=new HotOrNot(this);
         ex.open();
         ex.updateEntry(lRow,mName,mHotness);
         ex.close();
}
catch(Exception e )
{

    String error = e.toString();
    Dialog d = new Dialog(this);
    d.setTitle("CATCH");
    TextView tv = new TextView(this);
    tv.setText(error);
    d.setContentView(tv);
    d.show();
}


    } else if (id == R.id.bSQLdelete){ 

        try{ String sRow1 = sqlRow.getText().toString();
         long lRow1= Long.parseLong(sRow1);
         HotOrNot ex1=new HotOrNot(this);
         ex1.open();
         ex1.DeletEntry(lRow1);
         ex1.close();
    }

catch(Exception e )
{

    String error = e.toString();
    Dialog d = new Dialog(this);
    d.setTitle("CATCH");
    TextView tv = new TextView(this);
    tv.setText(error);
    d.setContentView(tv);
    d.show();
}
    }
}

}

0 个答案:

没有答案