Ludwig
Ludwig

Reputation: 832

Using variables in redshift insert command - Python lambda function

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.

Upvotes: 1

Views: 573

Answers (1)

Marcin
Marcin

Reputation: 238467

You can use f-strings:

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

Upvotes: 1

Related Questions