需要更新的SQLite列的值SQLite

2023-09-07 16:43:55 作者:半生輕狂客

我需要否定的列的值,列中的所有的行。我想这样做,使用下面的查询,但没有更新任何行:

I need to negate the value of a column for all the rows in a column. I am trying to do so using the following query, but it is not updating any Row:

sqliteDatabase.rawQuery("UPDATE "+ SQL_TABLE_NAME + " SET "+ COL_1 +" = " + "-"+COL_1  , null);

如果COL-1 = COL_1,它的类型是整数的表。

Where COL-1 = col_1, which is of type integer in the table.

- 的Twinks

--Twinks

推荐答案

您可以乘以-1列

 Update SQL_TABLE_NAME Set COL_1 = COL_1 * -1

记住,您说,您的查询格式为我的作品;

Mind you saying that, your query format works for me;

 Update tPerson Set Age = -Age

如果我是你,我会创建一个字符串,并设置查询到,所以你可以检查它被设置为您期望的:

If I were you I'd create a string and set your query to that so you can check it's being set as you expect:

 String sql = "UPDATE "+ SQL_TABLE_NAME + " SET "+ COL_1 +" = " + "-" + COL_1;
 sqliteDatabase.rawQuery(sql, null); //breakpoint here, and check sql var

请问您SQL_TABLE_NAME有它的空间?尝试将其包装在方括号,也许同你列名。

Does your SQL_TABLE_NAME have a space in it? Try wrapping it in square brackets, and perhaps the same with your column name.

编辑:

我会建议更新主键是负面的,但要做到这一点,你可以做这样的事情:

I would recommend updating primary keys to be negative, but to accomplish this you could do something like this:

 SET IDENTITY_INSERT Person ON

 Insert Into Person ([Id], [Name], [Age])
 Select -[Id], [Name], [Age] From Person

 SET IDENTITY_INSERT Person OFF

(但是这会复制你的表,你可能需要将数据推到一个临时表) - 不过,我认为这是一个可怕的解决方案。我试图找到一种方式来调整你的表架构,所以它不是主键。

(but this will duplicate your table, you might want to shove the data into a temp table) - however I think this is a horrible solution. I'd try and find a way to restructure your table schema so it isn't the primary key.