sqlsyntaxinsertexecutemany

SQL OperationalError: near "%": syntax error when using inserting values into a table with "executemany"


I have been trying to insert values into a table with the following code:

top10_strong_beers.values.tolist() =
[['Surly Brewing Company', 'Abrasive Ale', 2020, 1],
 ['Modern Times Beer', 'Blazing World', 2020, 2],
 ['Sixpoint Craft Ales', 'Hi-Res', 2020, 3],
 ['Southern Star Brewing Company', 'Red Cockaded Ale', 2020, 4],
 ['Tallgrass Brewing Company', 'Ethos IPA', 2020, 5],
 ['Caldera Brewing Company', 'Hopportunity Knocks IPA', 2020, 6],
 ['Mike Hess Brewing Company', 'Habitus (2014)', 2020, 7],
 ['Oskar Blues Brewery', 'GUBNA Imperial IPA', 2020, 8],
 ['Renegade Brewing Company', 'Redacted Rye IPA', 2020, 9],
 ['Sockeye Brewing Company', 'Dagger Falls IPA', 2020, 10]]


query = """
INSERT INTO strong_beer_competition
    (brewery,beer,year,ranking)
     VALUES (%s,%s,%s,%s)
    """

cur.executemany(query, top10_strong_beers.values.tolist())
conn.commit()

I get the following error:

OperationalError                          Traceback (most recent call last)
<ipython-input-47-e3e92e2c1d6f> in <module>
     11 """
     12 
---> 13 cur.executemany(query, top10_strong_beers.values.tolist())
     14 conn.commit()

OperationalError: near "%": syntax error

I'm not sure how to fix this. I've been searching for why this error occurs, but it seems similar to other code I've looked up. Can anyone help? Anything that gets me closer to solving this is appreciated. Thank you.


Solution

  • Well, I accidentally found an answer when looking up a solution for another problem. I should be using "?" instead of "%s".

    I hadn't realised that there were different versions of SQL, and I am apparently using SQLite3 that uses "?" and not "%s". Hope this helps other idiots like myself.