简体   繁体   English

如何在卸载中转义单引号

[英]How to escape single quotes in Unload

    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)

I'm trying to execute the above script to read the table and then create a file in S3 我正在尝试执行上述脚本以读取表,然后在S3中创建文件

Since my columns are string I'm not able to skip the single quotes and I'm getting error as syntax error near where 由于我的列是字符串,所以我无法跳过单引号,并且在语法错误的附近获取错误

Also, I've tried giving \\ in where condition still it showing the same error. 另外,我尝试在条件仍然显示相同错误的地方给\\。

Any help would be highly appreciated. 任何帮助将不胜感激。

Thanks 谢谢

As Sarang says, simply by replacing single quotes by double quotes in col4 and col5 values of your query should do the trick. 正如Sarang所说,只需在查询的col4和col5值中用双引号替换单引号即可解决问题。

However I would suggest you to break your string down in smaller chunks easier to read and maintain. 但是,我建议您将字符串分解为较小的块,以便于阅读和维护。 This way, you should be able to use execute as chepner suggests (and MySQL documentation ): 这样,您应该能够按照chepner的建议(和MySQL文档 )使用execute

# 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)

' (single quotes can be sent as ) -> \\\\\\\\' '(单引号可以作为发送)-> \\\\\\\\'

I had used this in the R as well as python Please find solutions 我在R和python中都用过这个,请找到解决方案

if your sql QUERY is 如果您的SQL查询是

Select * from sample_table where register_date='2018-12-31' 从sample_table中选择*,其中register_date ='2018-12-31'

then for unload command write it like this 然后对于卸载命令这样写

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)

You can also use postgres style : 您也可以使用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='
;

You can put values in double quotes. 您可以将值放在双引号中。 'select col1,col2 from %s.visitation_hourly_summary_us where col4= "2018-07-10" and col5= "1";' '从%s.visitation_hourly_summary_us中选择col1,col2,其中col4 =“ 2018-07-10”和col5 =“ 1”;'

You would want to use two single quotes to enclose the value. 您可能希望使用两个单引号将值引起来。

If your query contains quotes (for example to enclose literal values), put the literal between two sets of single quotation marks—you must also enclose the query between single quotation marks: 如果您的查询包含引号(例如,将文字值括起来),则将文字放在两组单引号之间—您还必须将查询括在单引号之间:

Example: 例:

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

Taken from the redshift documentation: https://docs.aws.amazon.com/redshift/latest/dg/r_UNLOAD.html 取自redshift文档: https : //docs.aws.amazon.com/redshift/latest/dg/r_UNLOAD.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM