简体   繁体   English

Android更新所有记录SQLite

[英]Android update all records SQLite

my table contains 7 records. 我的表包含7条记录。 With the update method, I want to update a column that has INTEGER values . 使用update方法,我想更新具有INTEGER值的列。 with a for loop, I want to update all records with a sequence of numbers, but it seems not work properly, because all records are updated with the last of the loop (7). 使用for循环,我想用一系列数字更新所有记录,但它似乎无法正常工作,因为所有记录都使用循环的最后一个更新(7)。 Maybe the error is in the WHERE clause? 也许错误发生在WHERE子句中? Thanks for your advice. 谢谢你的建议。

SQLiteDatabase datab = new DatabaseHelper(getActivity()).getReadableDatabase();

ContentValues cv = new ContentValues();
for(int i = 1; i<8; i++){
    cv.put(MyTable.NUMERAZIONE, i);
    datab.update(MyTable.TABLE_NAME, cv, MyTable.NUMERAZIONE + "!="+i, null);
}
datab.close();

I also tried without a WHERE clause, but I get the same result: 我也试过没有WHERE子句,但我得到了相同的结果:

datab.update(MyTable.TABLE_NAME, cv, null, null);

Records are 7, I want to update all records, without any clause. 记录是7,我想更新所有记录,没有任何条款。

Problem is with your whereClause. 问题在于你的whereClause。

MyTable.NUMERAZIONE + "!="+i

means that it will update all records that not equal current i value (in that case it will update all records to value i). 意味着它将更新所有不等于当前i值的记录(在这种情况下,它将所有记录更新为值i)。

EDIT 编辑

datab.update(MyTable.TABLE_NAME, cv, null, null);

will also update each time all rows, so it is not a solution. 每次都会更新所有行,所以它不是解决方案。

I am not an expert in sqlite, but one of solutions is to fetch all rows, iterate through it, set new i value and update on db. 我不是sqlite的专家,但其中一个解决方案是获取所有行,遍历它,设置new i值并在db上更新。

Consider the SQL that will be run by your call to update() . 考虑一下您对update()调用将运行的SQL。 Let's take the first iteration (i = 1): 让我们进行第一次迭代(i = 1):

UPDATE table_name SET numerazione = 1 WHERE numerazione != 1

Any row where numerazione is not 1 will have it set to 1, so the result is that every row has numerazione = 1. Now we run the next iteration (i = 2): numerazione不为1的任何行都将其设置为1,因此结果是每行都有numerazione = 1.现在我们运行下一次迭代(i = 2):

UPDATE table_name SET numerazione = 2 WHERE numerazione != 2

Any row where numerazione is not 2 will have it set to 2. But after the previous iteration, all rows have numerazione = 1, so this statement will update all the rows. numerazione不是2的任何行都将它设置为2.但是在上一次迭代之后,所有行都有numerazione = 1,因此该语句将更新所有行。 Same thing happens for the remaining iterations, with the last one setting numerazione to 7 for all rows. 对于剩余的迭代,同样的事情发生,最后一个设置为所有行的numerazione为7。

You need to find some other WHERE clause or some other way to update your rows. 您需要找到一些其他WHERE子句或其他一些方法来更新您的行。 One solution is to build the WHERE clause using the NOT IN operator and making the list larger each time. 一种解决方案是使用NOT IN运算符构建WHERE子句,并且每次都使列表更大。 The first would have NOT IN (1) , the second would have NOT IN (1,2) , and so on. 第一个NOT IN (1) ,第二个NOT IN (1,2) ,依此类推。 Since you only have to do this a small number of times, it won't be too bad for performance. 由于您只需要做很少次,所以性能不会太差。

private String buildWhere(int number) {
    StringBuilder builder = new StringBuilder(MyTable.NUMERAZIONE).append( "NOT IN (1");
    for (int i = 2; i <= number; i++) {
        builder.append(',').append(i);
    }
    builder.append(')');
    return builder.toString();
}
...
datab.update(MyTable.TABLE_NAME, cv, buildWhere(i), null);

This should result in the following statements: 这应该导致以下声明:

UPDATE table_name SET numerazione = 1 WHERE numerazione NOT IN (1)
UPDATE table_name SET numerazione = 2 WHERE numerazione NOT IN (1,2)
...
UPDATE table_name SET numerazione = 7 WHERE numerazione NOT IN (1,2,3,4,5,6)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM