SQL SQLite 中的 UPDATE 语法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13482091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UPDATE syntax in SQLite
提问by calbertts
I need to know if I can do this in an UPDATE statement:
我需要知道是否可以在 UPDATE 语句中执行此操作:
UPDATE users SET ('field1', 'field2', 'field3')
VALUES ('value1', 'value2', 'value3');
Or similar syntax. I'm using SQLite.
或类似的语法。我正在使用 SQLite。
Note:
笔记:
Nobody understands me, I just want to know if it is possible to SET separate fields to separate values. That's all.
没有人理解我,我只是想知道是否可以将单独的字段设置为单独的值。就这样。
回答by ypercube??
There is a (standard SQL) syntax that is similar to what you propose but as far as I know, only Postgres has implemented it:
有一种(标准 SQL)语法与您提出的类似,但据我所知,只有 Postgres 实现了它:
UPDATE users
SET (field1, field2, field3)
= ('value1', 'value2', 'value3')
WHERE some_condition ;
Tested (for the infidels) in: SQL-Fiddle
测试(对于异教徒):SQL-Fiddle
This also works in Postgres:
这也适用于 Postgres:
UPDATE users AS u
SET
(field1, field2, field3)
= (f1, f2, f3)
FROM
( VALUES ('value1', 'value2', 'value3')
) AS x (f1, f2, f3)
WHERE condition ;
This works in Postgres and SQL-Server:
这适用于 Postgres 和 SQL-Server:
UPDATE users
SET
field1 = f1, field2 = f2, field3 = f3
FROM
( VALUES ('value1', 'value2', 'value3')
) AS x (f1, f2, f3)
WHERE condition ;
and as @HymanDouglas commented, this works in Oracle:
正如@HymanDouglas 所评论的,这适用于 Oracle:
UPDATE users
SET (field1, field2, field3)
= ( SELECT 'value1', 'value2', 'value3' FROM dual )
WHERE condition ;
回答by Taryn
What you posted is not the correct syntax for UPDATE
. To UPDATE
your syntax is:
您发布的内容不是UPDATE
. 为了UPDATE
你的语法是:
UPDATE users
SET field1 = 'value1',
field2 = 'value2',
field3 = 'value3';
You will want to add a WHERE
clause or this will UPDATE
all records.
您将要添加一个WHERE
子句,否则这将是UPDATE
所有记录。
回答by RichardTheKiwi
If you want to update a record, it should look like this. You should very rarely want to update without a WHERE condition.
如果要更新记录,它应该如下所示。您应该很少想在没有 WHERE 条件的情况下进行更新。
UPDATE users
SET field1='value1',
field2='value2',
field3='value3'
WHERE field1=1
SQL Fiddle
SQL小提琴
回答by Sklivvz
Try
尝试
UPDATE users SET field1='value1', field2='value2'
etc...
等等...
You can find the official docs here.
您可以在此处找到官方文档。
W3Schools also has a useful page:
W3Schools 也有一个有用的页面:
SQL UPDATE Statement
The UPDATE statement is used to update records in a table.
SQL UPDATE Syntax
SQL 更新语句
UPDATE 语句用于更新表中的记录。
SQL 更新语法
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
Note: Notice the WHERE clause in the UPDATE syntax. The WHERE clause specifies which record or records that should be updated. If you omit the WHERE clause, all records will be updated!
注意:注意 UPDATE 语法中的 WHERE 子句。WHERE 子句指定应该更新哪个或哪些记录。如果省略 WHERE 子句,则所有记录都将被更新!
EDIT: since you seem to need to create a string, the normal method to do so safely is to use prepared statements.
编辑:因为您似乎需要创建一个字符串,所以安全地这样做的正常方法是使用准备好的语句。
Assuming Java, we already have this exampleon StackOverflow:
假设使用 Java,我们已经在 StackOverflow 上有这个例子:
SQLiteDatabase db = dbHelper.getWritableDatabase();
SQLiteStatement stmt = db.compileStatement("SELECT * FROM Country WHERE code = ?");
stmt.bindString(1, "US");
stmt.execute();
In your case,
在你的情况下,
SQLiteDatabase db = dbHelper.getWritableDatabase();
SQLiteStatement stmt = db.compileStatement("UPDATE users SET field1=?, field2=?...");
stmt.bindString(1, "value1");
stmt.bindString(2, "value2");
stmt.execute();
回答by ingobaab
No, there is nosuch syntax in sqlite (postgress seems to have such thing implemented) - but better use:
不,sqlite 中没有这样的语法(postgress 似乎实现了这样的东西) - 但更好地使用:
UPDATE users SET firstname = 'Joe', lastname = 'value2', age = 50 WHERE id=12;