?
Solved

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

Posted on 2016-09-20
8
Medium Priority
?
32 Views
Last Modified: 2016-09-25
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
Comment
Question by:mickt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 1500 total points
ID: 41806357
Do you have anything working?  Which MySQL driver are you using?

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

Author Comment

by:mickt
ID: 41806368
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
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 41806382
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Author Comment

by:mickt
ID: 41806409
Sorry, I'm not following, can you please clarify for me?
0
 

Author Comment

by:mickt
ID: 41806609
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
 

Accepted Solution

by:
mickt earned 0 total points
ID: 41806954
II resolved as follows:

    tbl = op.execute("SHOW TABLES LIKE 'table_name'")
    if not tbl:
        return
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 41807226
Glad you got it figured out.  I had to get some sleep.
0
 

Author Closing Comment

by:mickt
ID: 41814583
Answers question.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The really strange introduction Once upon a time there were individuals who intentionally put the grass seeds to the soil with anticipation of solving their nutrition problems. Or they maybe only played with seeds and noticed what happened... Som…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question