Escape table name in SQLite?

2019-05-22 11:01发布

问题:

I have table named References in SQLite, so I can't target it, it seems. SQLite studio I use to edit databases throws an error.

Is there a way to escape database name?

The query is:

UPDATE References 
SET DateTimeLastEdited = datetime('now', 'localtime')
WHERE NewsItemID = old.NewsItemID;

(This is part of the trigger I am making.)

回答1:

You can escape table names with double quotes:

UPDATE "References" SET DateTimeLastEdited = datetime('now', 'localtime') WHERE NewsItemID = old.NewsItemID;

Depending on what you want to escape, you need to use different delimiters:

If you want to use a keyword as a name, you need to quote it. There are four ways of quoting keywords in SQLite:

'keyword' A keyword in single quotes is a string literal.

"keyword" A keyword in double-quotes is an identifier.

[keyword] A keyword enclosed in square brackets is an identifier. This is not standard SQL. This quoting mechanism is used by MS Access and SQL Server and is included in SQLite for compatibility.

`keyword` A keyword enclosed in grave accents (ASCII code 96) is an identifier. This is not standard SQL. This quoting mechanism is used by MySQL and is included in SQLite for compatibility.

From SQLite documentation