Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

whereArgs SQLite database delete

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?

like image 660
locoboy Avatar asked Mar 21 '11 02:03

locoboy


3 Answers

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.

like image 62
Mudassir Avatar answered Oct 22 '22 01:10

Mudassir


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.

like image 33
Derzu Avatar answered Oct 22 '22 01:10

Derzu


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 
    }
);
like image 28
TheSolarSheriff Avatar answered Oct 22 '22 02:10

TheSolarSheriff