Link to home
Start Free TrialLog in
Avatar of Randall-B
Randall-B

asked on

Syntax for a Variable in a MySQL Query in Python

In this MySQL query, I want to use a variable for the DocNumber, instead of the hardcoded '99':

    cursor.execute("SELECT DocText FROM compare WHERE DocNumber = '99' ORDER BY Posted ASC limit 1")

I tried this, but it did not work:

   Doc = '99'
. . .
   cursor.execute("SELECT DocText FROM compare WHERE DocNumber = 'Doc' ORDER BY Posted ASC limit 1")

In Python, what is the proper syntax for using a variable inside a double-quoted sql statement in the cursor.execute function?
ASKER CERTIFIED SOLUTION
Avatar of RichieHindle
RichieHindle

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Randall-B
Randall-B

ASKER

Yes. That works great. Thanks!