When to use single quotes in an SQL statement?

久未见 提交于 2019-12-12 13:14:09

问题


I know that I should use it when I deal with data of TEXT type (and I guess the ones that fall back to TEXT), but is it the only case?

Example:

UPDATE names SET name='Mike' WHERE id=3

I'm writing an SQL query auto generation in C++, so I want to make sure I don't miss cases, when I have to add quotes.


回答1:


Single quotes (') denote textual data, as you noted (e.g., 'Mike' in your example). Numeric data (e.g., 3 in your example), object (table, column, etc) names and syntactic elements (e.g., update, set, where) should not be wrapped in quotes.




回答2:


The single quote is the delimiter for the string. It lets the parser know where the string starts and where it ends as well as that is is a string. You will find that sometimes you get away with a double quote too.

The only way to be certain you don't miss any cases would be to escape the input, otherwise this will be vulnerable to abuse when somehow a single quote ends up in in the text.



来源:https://stackoverflow.com/questions/32926814/when-to-use-single-quotes-in-an-sql-statement

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!