pythonsqldb2

Running multiple SQL-Statements with Python


im looking to run multiple SQL-Statements with Python. To run one Statement works, but if i want to run Multiple Statements only the first one works:

This is what i tried so far:

# Name of the DB2-Table
table_name = 'Table1'

# SQL-Statement
sql = f"""
    TRUNCATE TABLE {table_name} IMMEDIATE; -- Clear the table
    INSERT INTO {table_name} (Year, UserNr, Username, Age) -- Insert Values
    VALUES (?, ?, ?, ?)
"""

The Problem: Only the first Truncate Statement works. Means the Table will be Empty after running the Script. Is there a way to run both statements?

Edited: I'm using pyodbc and pandas. DB-Server Platform is linux.


Solution

  • pyodbc doesn't support multiple statement in a single line you have to break your query and execute one-by-one.