基于当前的列值的Andr​​oid更新的数据库列 [英] android update database column based on the current column value

查看:89
本文介绍了基于当前的列值的Andr​​oid更新的数据库列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android中, SQLiteDatabase 有一个更新功能

In android, SQLiteDatabase has a update function

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

酒店在提出了新的价值 如果我想通过添加一个到它,更新列中的我应该怎么prepare的ContentValues​​值的变量?我不认为有以下会的工作。

new values in put in values
If I want to update a column A by adding one to it, how should I prepare the ContentValues values variable? I don't think the following would work.

cv.put("A", "A" + 1);

我可以肯定跑 execSQL 与原始SQL,但它不会返回NUM行更新

I can sure run execSQL with raw sql, but it does not return num of row updated

推荐答案

如果你执行一个原始查询,这样的事情应该努力增加在列的当前值:

If you'd execute a raw query, something like this should work to increment the current value in the column:

UPDATE table_name SET column_a = column_a + 1 WHERE _id = 1

(其中 1 只是一个例子来说明如何将其应用到特定的行)

(where 1 is just an example to illustrate how to apply it to a specific row)

同样可能不会与 ContentValues​​工作,因为(顾名思义)它需要的值来设置列。这意味着它需要建立之前已经被评估的 ContentValues​​ ,而具有一个原始查询的值不计算直到查询实际运行在数据库上。

The same probably wouldn't work with ContentValues, since (as the name indicates) it takes the values to set the column to. That means it needs to have been evaluated before building the ContentValues, whereas with a raw query the value isn't evaluated until the query actually runs on the database.

您可以先检索当前值,然后发布一个更新时,增加了相应的;这需要选择查询第一。相当普遍,虽然,你正在使用的Java对象,其中一个行的列值绑定到对象的成员领域。如果你有这样的设置,那么你可能已经在瞬间电流值要运行一个更新查询。

You can of course retrieve the current value first and then increment that accordingly when issuing an update; that requires a select query first. Quite commonly though, you're working with objects in Java, where the column value for a row is bound up to a member field of the object. If you've got a setup like that, then you probably already have the current value at the moment you want to run an update query.

因此​​,它只是看起来有点像:

As such, it would just look somewhat like:

SomeObject object = ...;
cv.put("column_a", object.getSomeValue() + 1);

(这里我假设 object.getSomeValue()将返回 INT

//编辑:下面是原查询方法更多的例子:

// edit: here's some more examples for the raw query approach:

// EDIT2:编辑完你原来的问题,并补充说:

// edit2: You've edited your original question and added:

我可以肯定跑 execSQL 与原始SQL,但它并没有返回NUM   行更新

I can sure run execSQL with raw sql, but it does not return num of row updated

如果知道有多少行查询更改为一个的必须的,那么你可能会利用的变化()功能。这仍然意味着你将不得不虽然运行第二个查询。

If knowing how many rows the query changed is a must, then you can potentially leverage the changes() function. It still means you're going to have to run a second query though.

SELECT changes() FROM table_name

该文档说:

()函数返回的是数据库中的行数的变化   通过最近完成的INSERT改变或插入或删除,   DELETE或UPDATE语句,其中不包括低级别的语句   触发器。这些变化()SQL函数是围绕着一个包装   sqlite3_changes()C / C ++函数,因此遵循相同的规则   计数的变化。

The changes() function returns the number of database rows that were changed or inserted or deleted by the most recently completed INSERT, DELETE, or UPDATE statement, exclusive of statements in lower-level triggers. The changes() SQL function is a wrapper around the sqlite3_changes() C/C++ function and hence follows the same rules for counting changes.

另外,你可以看看的<一个href="http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#rawQuery%28java.lang.String,%20java.lang.String%5b%5d%29"相对=nofollow> rawQuery() 方法接受一个SQL语句并返回结果为光标。不知道,如果它甚至适用于一个更新查询,或结果是否会是什么明智的,但如果你真的很幸运,你可能会发现 Cursor.getCount()为您提供了受影响的行数。

Alternatively, you could look into the rawQuery() method that takes an SQL statement and returns the result as a Cursor. Not sure if it that even works for an update query, or whether the result would be anything sensible, but if you're really lucky, you may find that Cursor.getCount() gives you the number of affected rows.

这篇关于基于当前的列值的Andr​​oid更新的数据库列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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