Java 无法在android中的数据库中运行更新查询?

Java 无法在android中的数据库中运行更新查询?,java,android,sqlite,Java,Android,Sqlite,我正在尝试更新联系人表的星号字段,或者换句话说,我正在尝试向我的应用程序中的收藏夹列表添加一个名称。我搜索了很多,但找不到任何特定于我的案例的内容。更新查询只是不做任何更改,甚至没有引发异常,堆栈跟踪中没有任何内容 //appname used in the code is the name which I need to add to the starred list.. ContentResolver cr = getContentResolver(); Cursor

我正在尝试更新联系人表的星号字段,或者换句话说,我正在尝试向我的应用程序中的收藏夹列表添加一个名称。我搜索了很多,但找不到任何特定于我的案例的内容。更新查询只是不做任何更改,甚至没有引发异常,堆栈跟踪中没有任何内容

//appname used in the code is the name which I need to add to the starred list.. 
ContentResolver cr = getContentResolver();          

Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
            null, null, null, null);

ContentValues value=new ContentValues();
value.put("ContactsContract.Contacts.STARRED",1);

cur.moveToFirst();  
while(!cur.isLast()){

        String fetchname=cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

        if((appname.equalsIgnoreCase(fetchname))==true)
        {    
            String starredvalue=cur.getString(cur.getColumnIndex(ContactsContract.Contacts.STARRED));
            try
                {                  
                cr.update(ContactsContract.Contacts.CONTENT_URI,value,"starred=?",new String[]{starredvalue});
            }
            catch(Exception e)
            {
                e.printStackTrace();                    
            }
       }//if  
       cur.moveToNext();     
}//while
从:

public intupdate(字符串表、ContentValues值、字符串where子句、字符串[]wherergs)

WHERE子句更新时应用的可选WHERE子句。传递null将更新所有行

您的WHERE子句如下所示:

"starred='true'"
请注意,在该SQL片段中没有任何占位符(例如,
col=?
)?但您正在为wherergs参数中不存在的占位符提供参数:

new String[]{starredvalue}
因此,
update
调用正在查找占位符1,而不是在where子句中找到它,然后您会得到一个“索引超出范围”异常


我知道你想做什么,但也许你希望where子句是
“starred=?”
(而不是
“starred='true'”
)或者是
null
wherergs.

cr.update(Contacts合同.Contacts.CONTENT_URI,value,“starred='true',null);我试过了,例外情况消失了,但仍然没有出现。顺便说一句,谢谢你的回答。@prof_jack:你试过带wherergs的
“starred=?”
?或者您可能想要
“starred=1”
和一个
null
其中,SQLite对booleans.cr.update使用一和零(ContactsContract.Contacts.CONTENT\u URI,value,ContactsContract.Contacts.starred+“=?”,新字符串[]{starredvalue});是的,我也试过,和上面一样,异常已消失,但星号字段未得到更新。我尝试在编辑的代码中用值1代替true。我已经做了几天了,但找不到任何东西。如果你有什么想法,请告诉我。谢谢。@prof_jack:你能直接查看数据库,看看这些值是什么吗
starred
栏目真的有?并且可能在您的问题中包含表模式。