简体   繁体   English

如何将变量传递给 python 中的 sql 查询?

[英]How to pass the variable to the sql query in the python?

I am trying to pass the input arguments to the AWS SQL Load command, but I am getting a wrong SQL syntax error.我正在尝试将输入 arguments 传递给 AWS SQL 加载命令,但我收到了错误的 SQL 语法错误。 Can someone plz tell me where am I making a mistake?有人可以告诉我我在哪里犯错了吗?

tableName = sys.argv[1]
fileName = sys.argv[2]

curr.execute("""
    LOAD DATA FROM S3 's3-region://example-bkt/(%s)'
    INTO TABLE test_schema.(%s);""", (fileName,tableName))

This is the error which I am getting:这是我得到的错误:

mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'TEST_FILE')'

Your command is not properly formatted.您的命令格式不正确。 Try one of these options (second option is python3's improved string formatting):尝试以下选项之一(第二个选项是 python3 改进的字符串格式):

command = "LOAD DATA FROM S3 's3-region://example-bkt/%s' INTO TABLE test_schema.%s;" % (fileName,tableName)

command = f"LOAD DATA FROM S3 's3-region://example-bkt/{fileName}' INTO TABLE test_schema.{tableName};"

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

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