whereArgs SQLite数据库删除

时间:2011-03-21 02:58:54

标签: android sqlite delete-row

我正在尝试以编程方式删除sqlite数据库中的几行for android,我想知道whereArgs在本文档中引用了什么:

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

有人可以举个例子吗?

3 个答案:

答案 0 :(得分:16)

whereArgs是where子句的值。当你使用“?” (占位符)在第二个参数(whereClause)中,您必须提供whereArgsSee this post for details

答案 1 :(得分:3)

将“=?”放在第二个参数(whereClause)的末尾,如下所示:

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

number of ?是参数的数量。

答案 2 :(得分:2)

占位符?不工作(buggy) - 所以你需要构造whereClause(选择)并发送空参数(selectionArgs)

e.g。使用用户搜索文本加载动态列表:

    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

        }
        });

并在您的ManageMyCustomerMenuList()查询代码中添加以下内容:

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 
    }
);