从数据库获取和删除行[android]

时间:2014-06-24 12:20:39

标签: android database sqlite

我想从数据库中获取一行并在MyActivity类中显示它。但我想按行的位置来做。与删除相同的问题。如果我点击列表项我想删除该位置(简单行)。

public class MyActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        DatabaseHandler db = new DatabaseHandler(this);

        /**
         * CRUD Operations
         * */
        // Inserting Contacts
        Log.d("Insert: ", "Inserting ..");
        db.addContact(new Contact("Ravi", "9100000000"));
        db.addContact(new Contact("Srinivas", "9199999999"));
        db.addContact(new Contact("Tommy", "9522222222"));
        db.addContact(new Contact("Karthik", "9533333333"));

        List<Contact> contacts = db.getAllContacts();

        // here i want to get a simple contact by its position in table. 
        //  here i want to delete a simple contact by its position in table.   

}

这是我的DatabaseHandler:

public class DatabaseHandler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "contactsManager";
    private static final String TABLE_CONTACTS = "contacts";
    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "name";
    private static final String KEY_PH_NO = "phone_number";

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

    // Adding new contact
    void addContact(Contact contact) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_NAME, contact.getName()); // Contact Name
        values.put(KEY_PH_NO, contact.getPhoneNumber()); // Contact Phone

        // Inserting Row
        db.insert(TABLE_CONTACTS, null, values);
        db.close(); // Closing database connection
    }

    // Getting single contact
    Contact getContact(int id) {
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
                KEY_NAME, KEY_PH_NO }, KEY_ID + "=?",
                new String[] { String.valueOf(id) }, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();

        Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
                cursor.getString(1), cursor.getString(2));
        // return contact
        return contact;
    }       

    // Deleting single contact
    public void deleteContact(Contact contact) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_CONTACTS, KEY_ID + " = ?",
                new String[] { String.valueOf(contact.getID()) });
        db.close();
    }

}

你能帮助我吗?

1 个答案:

答案 0 :(得分:1)

选择所有数据:下面是函数返回光标。你必须从光标中获取价值并根据你的要求展示。

public Cursor selectAll() {

    String query = "SELECT * FROM TBLevels";
    System.out.println(query);
    cur = db.rawQuery(query, null);

    cur.moveToFirst();
    return cur;

}

显示值: -

        Routines r=new Routines(PhaseBuilder.this);
        r.open();
        Cursor cr=r.selectAll();
        for(int i=0;i<cr.getCount();i++)
        {
            System.out.println(cr.getString(0));// 0 indicate your column 
            System.out.println(cr.getString(1));// 1 indicate your column 
            cr.moveToNext();
        }
        cr.close();
        r.close();  

选择特定数据

public Cursor getrecord(String LEVELID) {
    //Log.e("we are", "TBLevels");
    Cursor mCursor = db.query("TBLevels", new String[] {
            TB_LEVELID, TB_LEVELTEXT }, 
            TB_LEVELID + "=" + "'" + LEVELID
            + "'", null, null, null, null, null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

删除数据

    public void deleteId(String ROUTINEID)
    {
        try
        {
            db.delete("TBRoutines", TB_ROUTINEID + "=" + ROUTINEID, null);
        }
        catch (Exception e)
        {
            // TODO: handle exception
            System.out.println("deleteId RW");
            e.printStackTrace();
        }
    }