1

How would I combine the following two statements to create a valid SQL query?

provider = os.path.basename(file)

cursor.execute("""INSERT into main_app_financialstatements 
                  (statement_id, provider_id***, url, date) 
                  VALUES (%s, %s***, %s, %s)""", 
                  (statement_id, provider***, url, date))

provider_id = SELECT id FROM main_app_provider WHERE provider=provider

In other words, I have the provider, and I need to SELECT the provider_id from another table in order to INSERT it into the main_app_financialstatements.

2 Answers 2

1
cursor.execute("""INSERT into main_app_financialstatements
  (statement_id, provider_id, url, date)
VALUES (%s, (SELECT id FROM main_app_provider WHERE provider=%s), %s, %s)""",
  (statement_id, provider, url, date))
Sign up to request clarification or add additional context in comments.

Comments

1

You could use the INSERT ... SELECT ... FROM variant of the INSERT command:

provider = os.path.basename(file)

sql = """
    INSERT INTO main_app_financialstatements 
        (statement_id, provider_id, url, date)
    SELECT %s, id, %s, %s
    FROM main_app_provider
    WHERE provider = %s
    """ 
args = (statement_id, url, date, provider)
cursor.execute(sql, args)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.