0

Hey guys using pymysql here.

What is wrong with this syntax?

cursor.execute("SELECT laptopname FROM laptops WHERE laptops.idlaptops NOT IN (SELECT idlaptops FROM bookings WHERE (booked=1) AND (datetime >= %s AND datetime <= %s)", (datetimeout,datetimein))

Thank you!

Edit: The error report: pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1")

Edit 2: Solved but also, how do I make it so my WHERE statement doesn't treat datetime as a column but as a variable/input?

Edit 3: Rápli András's answer was very useful, thanks!!!

1 Answer 1

2

datetime is a reserved word in SQL, you can't use that as column name or you have to put it in backticks like this:

`datetime`
Sign up to request clarification or add additional context in comments.

4 Comments

Ah okay thank you. It was also that I left a bracket off the 2nd SELECT statement I just noticed. How do I not refer to datetime as a column but just a piece of inputted data?
I'm not sure what you mean by that. Do you have a variable called datetime or what? Use the %s syntax as you did before.
Nevermind, I just realised how retarded I was being lol. Thank you.
datetime is a KEYWORD but it is not a RESERVED WORD in MySQL. The list is here: dev.mysql.com/doc/refman/5.7/en/keywords.html. I don't recommend using it as the name of an identifier, but it should work.