Search code examples
pythonpyodbc

Parameter markers in SQL Query


I'm new here and with Python, and tried to experiment with the module pyodbc. The goal of the script below is that I can add two values to the table weightdata.

The variables are:

  1. A timestamp
  2. The weight

I want to create a new row in the table weightdata with the input(). and execute a SQL query with the two variables.

Thanks in advance!

Problem I get te following error, but I don't know what I'm doing wrong?

('The SQL contains 0 parameter markers, but 2 parameters were supplied', 'HY000')

I tried to search the answer on the internet, but didn't found the answer I'm looking for.

Code

def insertweight():
    cursor = conn.cursor()
    weight = input("What is your weight today?: ")
    date = datetime.now()
    timestamp = datetime.timestamp(date)
    cursor.execute("INSERT INTO weightdata VALUES (%s, %s)",timestamp, weight)
    
insertweight()

Solution

  • pyODBC uses qmark placeholders (docs).

    def insertweight():
        cursor = conn.cursor()
        weight = input("What is your weight today?: ")
        date = datetime.now()
        timestamp = datetime.timestamp(date)
        cursor.execute("INSERT INTO weightdata VALUES (?, ?)", timestamp, weight)
        
    insertweight()