How can I check the result of mysql query in python?

mickt
mickt used Ask the Experts™
on
I've seen lines like op.execute("CREATE LANGUAGE plpgsql") in other scripts in my system so thought I could do similar with the following.

op.execute("SHOW TABLES LIKE 'table_name'")

How can I check the result from this and then do stuff based on existence or not?

I tried result = op.fetchone() but this is incorrect.
  AttributeError: 'module' object has no attribute 'fetchone'

and
  if not op.execute("SHOW TABLES LIKE 'agents'"):
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Dave BaldwinFixer of Problems
Most Valuable Expert 2014
Commented:
Do you have anything working?  Which MySQL driver are you using?

http://dev.mysql.com/downloads/connector/python/

Author

Commented:
Server version: 5.6.31-ndb-7.4.12-cluster-gpl

Multiple DB and all working.

Trying to do/not do something based on the existence of a table.
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
I believe you have to process 'SHOW TABLES' and similar queries just like a SELECT query because that is what they are.
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Author

Commented:
Sorry, I'm not following, can you please clarify for me?

Author

Commented:
I cam across this too.

    cursor.execute("SHOW TABLES LIKE 'table_name'")
    result = cursor.fetchone()
    if result:
        "There is a table named table_name."
    else:
        do stuff

but it complains about cursor
Commented:
II resolved as follows:

    tbl = op.execute("SHOW TABLES LIKE 'table_name'")
    if not tbl:
        return
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
Glad you got it figured out.  I had to get some sleep.

Author

Commented:
Answers question.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial