【问题标题】:How to escape single quotes in Unload如何在卸载中转义单引号
【发布时间】:2018-09-25 12:40:01
【问题描述】:
    conn_string = "dbname='{}' port='{}' user='{}' password='{}' host='{}'"\
            .format(dbname,port,user,password,host_url) 

    sql="""UNLOAD ('select col1,col2 from %s.visitation_hourly_summary_us where col4= '2018-07-10' and col5= '1';') TO 's3://%s/%s/%s.csv' \
            credentials 'aws_access_key_id=%s;aws_secret_access_key=%s' \
            MANIFEST GZIP ALLOWOVERWRITE;Commit;""" \
            % (schema_name,s3_bucket_name, schema,table,aws_access_key_id,\
            aws_secret_access_key)

con = psycopg2.connect(conn_string)
cur = con.cursor()
cur.execute(sql)

我正在尝试执行上述脚本来读取表格,然后在 S3 中创建一个文件

由于我的列是字符串,因此我无法跳过单引号,并且在 where 附近出现语法错误错误

另外,我尝试在 where 条件下给出 \ 仍然显示相同的错误。

任何帮助将不胜感激。

谢谢

【问题讨论】:

  • 您遇到的错误是什么?在我看来,您的conn_string 在这里是错误的
  • 另外,不要使用字符串格式化操作来构建查询。将参数化查询和参数元组分别传递给execute

标签: python amazon-web-services amazon-s3 amazon-redshift-spectrum


【解决方案1】:

你也可以使用 postgres 样式:

unload 
($$
select * from table where id='ABC'
$$)
to 's3://bucket/queries_results/20150324/table_dump/'
credentials 'aws_access_key_id=;aws_secret_access_key='
;

【讨论】:

  • 这种方法可以让您对查询进行语法高亮显示。
  • 这种方法避免了逃避字符地狱,谢谢!
【解决方案2】:

您可能希望使用两个单引号将值括起来。

如果您的查询包含引号(例如将文字值括起来), 将文字放在两组单引号之间——你必须 还要将查询用单引号括起来:

例子:

UNLOAD ('select * from venue where venuestate=''NV''')

取自红移文档: https://docs.aws.amazon.com/redshift/latest/dg/r_UNLOAD.html

【讨论】:

    【解决方案3】:

    正如 Sarang 所说,只需在查询的 col4 和 col5 值中用双引号替换单引号就可以了。

    但是,我建议您将字符串分解为更易于阅读和维护的小块。这样,您应该能够按照 chepner 的建议使用 execute(和 MySQL documentation):

    # Create the inner SQL statement. Notice the single quotes for the general
    # string and the double quotes for the col4 and col5 values
    sql_stmt = ('SELECT col1, col2 '
                'FROM %s.visitation_hourly_summary_us '
                'WHERE col4 = "2018-07-10" AND col5= "1";' % schema_name)
    
    # Format the s3 path
    s3_target = 's3://%s/%s/%s.csv' % (s3_bucket_name, schema, table)
    
    # Format credentials string
    s3_credentials = 'aws_access_key_id=%s;aws_secret_access_key=%s' % (
        aws_access_key_id, aws_secret_access_key)
    
    # Create a tuple with all preformatted strings
    data = (sql_stmt, s3_target, s3_credentials)
    
    # Format the s3 query skeleton
    s3_stmt = ("UNLOAD ('%s') TO '%s' "
               "CREDENTIALS '%s' "
               "MANIFEST GZIP ALLOWOVERWRITE;Commit;")
    
    con = psycopg2.connect(conn_string)
    cur = con.cursor()
    cur.execute(s3_stmt, data)
    

    【讨论】:

      【解决方案4】:

      '(单引号可以发送为)-> \\\\'

      我在 R 和 python 中都使用过这个 请寻找解决方案

      如果你的 sql QUERY 是

      Select * from sample_table where register_date='2018-12-31'

      然后对于卸载命令这样写

      sql=     """unload ('Select * from tnltemp.otpsuccess_details where register_date=\\\\'2018-12-31\\\\' ')
              to 's3://my-bucket/migration/exported_sample_table_' credentials 
              'aws_access_key_id=12234123;aws_secret_access_key=12345'
              DELIMITER AS ','
              NULL AS ''
              parallel off;""""
      
      
      
      cur = con.cursor()
      cur.execute(sql)
      

      【讨论】:

        【解决方案5】:

        您可以将值放在双引号中。 '从 %s.visitation_hourly_summary_us 中选择 col1,col2 其中 col4= "2018-07-10" and col5= "1";'

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-04-02
          • 2012-04-11
          • 2018-03-02
          • 2010-10-27
          • 2012-09-13
          相关资源
          最近更新 更多