4

I'm new to Python and working with SQL queries. I have a database that contains a table with meetings and their date along with an ID. What I want to do is check what meetings are happening on today's date. The code below results in showing all the meeting ID's that are happening on todays date. However I then want to check if a certain meeting ID is in the results, which I have stored as a variable, and if it is in there to carry out an IF function so I can then elaborate.

cur.execute("SELECT id FROM meeting WHERE DATE(starttime) = DATE(NOW())")

for row in cur.fetchall() :
    print row[0]
1
  • 1
    Not an answer to your question but you might want to invest some time learning a library like SQLAlchemy if you're going to do a lot of work with databases. It'll save a lot of manual effort working with cursor objects. Commented Apr 22, 2013 at 15:02

1 Answer 1

4

You can ask the database to tell you if the id is there:

cur.execute("SELECT id FROM meeting WHERE DATE(starttime) = DATE(NOW()) AND id=%s", (id,))
if cur.rowcount:
    # The id is there
else:
    # The id is not there.

I am assuming that you are using MySQLdb here; different database adapters use slightly different query parameter styles. Others might use ? instead of %s.

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

1 Comment

Ah thank you so much, that worked a treat! Sorry I was using MySQLdb,I should have mentioned. Really appreciate the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.