Why do we need to use 3 quotes while executing sql query from python cursor?

后端 未结 1 1315
天涯浪人
天涯浪人 2021-01-05 03:05

I came across some Python programs that connect to a MySQL database. In the code, I saw the query in the execute() function is enclosed withing 3 quotations(

相关标签:
1条回答
  • 2021-01-05 03:37

    It is not needed--the coder who made that just decided to use it for some reason (probably to add emphasis on that part).

    A triple-quoted string is just that: a string. It has the same properties as a regular string object. There are only two differences:

    1. It supports multiple lines. This saves you from having to place \n every time you want a newline.
    2. It allows you to have single and double quotes without escaping. This could be useful with SQL commands.

    In summary though, a triple-quoted string is a string:

    >>> type("""a""")
    <type 'str'>
    >>> type("a")
    <type 'str'>
    >>>
    

    and it is not needed in that code.

    0 讨论(0)
提交回复
热议问题