Python SQL query string formatting

后端 未结 10 1512
没有蜡笔的小新
没有蜡笔的小新 2020-12-04 08:51

I\'m trying to find the best way to format an sql query string. When I\'m debugging my application I\'d like to log to file all the sql query strings, and it is important t

相关标签:
10条回答
  • 2020-12-04 09:32

    For short queries that can fit on one or two lines, I use the string literal solution in the top-voted solution above. For longer queries, I break them out to .sql files. I then use a wrapper function to load the file and execute the script, something like:

    script_cache = {}
    def execute_script(cursor,script,*args,**kwargs):
        if not script in script_cache:
            with open(script,'r') as s:
                script_cache[script] = s
        return cursor.execute(script_cache[script],*args,**kwargs)
    

    Of course this often lives inside a class so I don't usually have to pass cursor explicitly. I also generally use codecs.open(), but this gets the general idea across. Then SQL scripts are completely self-contained in their own files with their own syntax highlighting.

    0 讨论(0)
  • 2020-12-04 09:34
    sql = ("select field1, field2, field3, field4 "
           "from table "
           "where condition1={} "
           "and condition2={}").format(1, 2)
    
    Output: 'select field1, field2, field3, field4 from table 
             where condition1=1 and condition2=2'
    

    if the value of condition should be a string, you can do like this:

    sql = ("select field1, field2, field3, field4 "
           "from table "
           "where condition1='{0}' "
           "and condition2='{1}'").format('2016-10-12', '2017-10-12')
    
    Output: "select field1, field2, field3, field4 from table where
             condition1='2016-10-12' and condition2='2017-10-12'"
    
    0 讨论(0)
  • 2020-12-04 09:36

    To avoid formatting entirely, I think a great solution is to use procedures.

    Calling a procedure gives you the result of whatever query you want to put in this procedure. You can actually process multiple queries within a procedure. The call will just return the last query that was called.

    MYSQL

    DROP PROCEDURE IF EXISTS example;
     DELIMITER //
     CREATE PROCEDURE example()
       BEGIN
       SELECT 2+222+2222+222+222+2222+2222 AS this_is_a_really_long_string_test;
       END //
     DELIMITER;
    
    #calling the procedure gives you the result of whatever query you want to put in this procedure. You can actually process multiple queries within a procedure. The call just returns the last query result
     call example;
    

    Python

    sql =('call example;')
    
    0 讨论(0)
  • 2020-12-04 09:36
    sql = """\
    select field1, field2, field3, field4
    from table
    where condition1=1
    and condition2=2
    """
    

    [edit in responese to comment]
    Having an SQL string inside a method does NOT mean that you have to "tabulate" it:

    >>> class Foo:
    ...     def fubar(self):
    ...         sql = """\
    ... select *
    ... from frobozz
    ... where zorkmids > 10
    ... ;"""
    ...         print sql
    ...
    >>> Foo().fubar()
    select *
    from frobozz
    where zorkmids > 10
    ;
    >>>
    
    0 讨论(0)
提交回复
热议问题