Inserting a string with double quotes into a table

后端 未结 3 1965
面向向阳花
面向向阳花 2021-01-12 10:13

I\'m using Oracle 10g and I\'m having a problem inserting a string with double quotes into a table. This is my statement

INSERT INTO USERS (ID, NAME, USERNAM         


        
相关标签:
3条回答
  • 2021-01-12 10:30

    A double quote is used to denote a quoted identifier, i.e. an object name that does not solely consist of alpha-numeric characters, $ and #. As an aside, it's recommended that you do not use quoted identifiers. This is the reason for your original ORA-00984 error. Oracle is assuming that "tes" is a column, not a string, and you can't use a column name in the VALUES clause of an INSERT statement, as explained in the error message.

    In order to insert the string "tes" into a table you need to ensure that it is quoted correctly:

    Character literals are enclosed in single quotation marks so that the database can distinguish them from schema object names.

    Any character can be part of a string so in order to insert a double quote into a table you need to enclose it within single quotes.

    insert into users (id, name, username) 
    values (null, '"tes"', '"hello"');
    

    Here's a SQL Fiddle to demonstrate.


    One additional thing to note. You state that this query is automatically generated, which means you may be vulnerable to SQL injection. I would highly recommend reading about bind variables in Guarding Against SQL Injection.

    0 讨论(0)
  • 2021-01-12 10:41

    It is possible. In Oracle, you quote string literals using single quotes.

    If you want to insert test into the database then you must quote that as 'test'.

    INSERT INTO USERS (NAME) VALUES ('test');
    

    If you want to insert "test" into the database then you must quote that as '"test"'.

    INSERT INTO USERS (NAME) VALUES ('"test"');
    
    0 讨论(0)
  • 2021-01-12 10:53

    Try wrapping the values inside single quotes.

    INSERT INTO USERS (ID, NAME, USERNAME) VALUES (NULL, '"tes"', '"hello"');
    
    0 讨论(0)
提交回复
热议问题