Home > Software engineering >  Using variables in redshift insert command - Python lambda function
Using variables in redshift insert command - Python lambda function

Time:08-24

I am trying to insert records to a redshift table using a lambda function. I am using

boto3.client('redshift-data')

for the same. Now I have the query as below.

query1 = "insert into dbname.tablename values('aaaa','bbbb','cccc')"

response = rsclient.execute_statement(
        ClusterIdentifier='xxxxx',
        Database='yyyy',
        DbUser='zzzz',
        Sql= query1,
        StatementName='examplestatement'
        )

This works fine. But I want to pass variables here instead of values. For instance,

var1 = 'aaaa'
var2 = 'bbbb'
var3 = 'cccc'

Then try the query as below but it it doesn't work, I think it something silly to do with quotes.

query1 = "insert into dbname.tablename values(var1,var2,var3)"

How can I achieve this. I write lambda function using python3. Any help is appreciated.

CodePudding user response:

You can use f-strings:

query1 = f"insert into dbname.tablename values('{var1}','{var2}','{var3}')"
  • Related