Android中的SQLite如何更新特定行 [英] SQLite in Android How to update a specific row

查看:116
本文介绍了Android中的SQLite如何更新特定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试更新一个特定的行一段时间,似乎有两种方法来做到这一点。从我读过和尝试过的,你可以使用:



execSQL(String sql) / p>



update(String table,ContentValues values,String whereClause,String [] whereArgs)方法。



(让我知道这是不正确的,因为我是Android的新手,而且非常新的) / p>

所以让我来看看我的实际代码。

  myDB.update (TableName,(Field1,Field2,Field3)+VALUES('Bob',19,'Male'),其中_id = 1,null); 

我试图完成这个:



更新Field1,Field2和Field3,其中主键(_id)等于1。



Eclipse在update下面给出了一个红线,并给出我这个解释:


类型
SQLiteDatabase中的方法update(String,ContentValues,String,String [])不是适用于参数(String,String,
String,null)


我猜我没有分配ContentValues正确。任何人都可以指向正确的方向?

解决方案

首先创建一个ContentValues对象:

  ContentValues cv = new ContentValues(); 
cv.put(Field1,Bob); //这些字段应该是实际列名的字符串值
cv.put(Field2,19);
cv.put(Field2,Male);

然后使用更新方法,现在应该工作:

  myDB.update(TableName,cv,_id =+ id,null); 


I've been trying to update a specific row for a while now, and it seems that there are two ways to do this. From what I've read and tried, you can just use the:

execSQL(String sql) method

or the:

update(String table, ContentValues values, String whereClause, String[] whereArgs) method.

(Let me know if this is incorrect as I am new to android and very new to SQL.)

So let me get to my actual code.

myDB.update(TableName, "(Field1, Field2, Field3)" + " VALUES ('Bob', 19, 'Male')", "where _id = 1", null);

I am trying to accomplish this:

Update Field1, Field2, and Field3 where the primary key (_id) is equal to 1.

Eclipse gives me a red line right underneath the word "update" and gives me this explanation:

The method update(String, ContentValues, String, String[]) in the type SQLiteDatabase is not applicable for the arguments (String, String, String, null)

I'm guessing I'm not assigning the ContentValues correctly. Can anyone point me in the right direction?

解决方案

First make a ContentValues object :

ContentValues cv = new ContentValues();
cv.put("Field1","Bob"); //These Fields should be your String values of actual column names
cv.put("Field2","19");
cv.put("Field2","Male");

Then use the update method, it should work now:

myDB.update(TableName, cv, "_id="+id, null);

这篇关于Android中的SQLite如何更新特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆