8

I'm trying to remove a couple of rows in a sqlite database programmatically for android and am wondering what the whereArgs are referring to in this documentation:

http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#delete(java.lang.String,%20java.lang.String,%20java.lang.String[])

Can someone give me an example?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
locoboy
  • 38,002
  • 70
  • 184
  • 260

3 Answers3

14

whereArgs are the values to the where clause. When you use the "?" (placeholder) in the second argument (whereClause), you've to provide the whereArgs. See this post for details.

Community
  • 1
  • 1
Mudassir
  • 13,031
  • 8
  • 59
  • 87
3

Put "=?" on the end of the second argument (whereClause), like this:

delete(CONTENT_URI, TEXT + "=?", new String [] { text } );

The number of ? is the number of arguments.

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
Derzu
  • 31
  • 1
2

Placeholders ? don't work (buggy) - so you need to construct the whereClause (selection) and send null arguments (selectionArgs)

e.g. To load a dynamic list from using user search text:

    mCustomerMenuList = (ListView)findViewById(R.id.customer_menu_list);
    mSearchText = (EditText)findViewById(R.id.autoCompleteTextView1);
    mSearchText.addTextChangedListener(new TextWatcher() {          

        public void afterTextChanged(Editable t) {
               //Reload the query using the search text
               ManageMyCustomerMenuList();         
               mCustomerMenuList.setAdapter(mAdapter);          
        }
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // Auto-generated method stub

        }
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            //  Auto-generated method stub

        }
        });

and in your ManageMyCustomerMenuList() query code place something like:

String s = mSearchText.getText().toString().toLowerCase();

String whereClause = Browser.BookmarkColumns.TITLE+" LIKE ?";
String whereArgs [] = new String[] {"%" +s+"%"};

mCustomerCursor = managedQuery(android.provider.Browser.BOOKMARKS_URI, 
    new String[] {
        Browser.BookmarkColumns._ID,
        Browser.BookmarkColumns.TITLE, 
        Browser.BookmarkColumns.URL
    }, whereClause, whereArgs, mSqlLimit
);

mAdapter = new SimpleCursorAdapter(this, R.layout.row_layout_test, 
    mCustomerCursor, new String[] {
        Browser.BookmarkColumns.TITLE,
        Browser.BookmarkColumns.URL
    }, new int[] { 
        R.id.test_layout_view1, 
        R.id.test_layout_view2 
    }
);
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
TheSolarSheriff
  • 129
  • 2
  • 2