简体   繁体   中英

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. 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):

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};"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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