• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 59
  • Last Modified:

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

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'"):
0
mickt
Asked:
mickt
  • 5
  • 3
2 Solutions
 
Dave BaldwinFixer of ProblemsCommented:
Do you have anything working?  Which MySQL driver are you using?

http://dev.mysql.com/downloads/connector/python/
0
 
micktAuthor 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.
0
 
Dave BaldwinFixer of ProblemsCommented:
I believe you have to process 'SHOW TABLES' and similar queries just like a SELECT query because that is what they are.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
micktAuthor Commented:
Sorry, I'm not following, can you please clarify for me?
0
 
micktAuthor 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
0
 
micktAuthor Commented:
II resolved as follows:

    tbl = op.execute("SHOW TABLES LIKE 'table_name'")
    if not tbl:
        return
0
 
Dave BaldwinFixer of ProblemsCommented:
Glad you got it figured out.  I had to get some sleep.
0
 
micktAuthor Commented:
Answers question.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now