[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2016-09-20
8
Medium Priority
?
36 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 84

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 84

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
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.

 

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 84

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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" www.python.org " You can also get information on Python scripting language from the above mentioned we…
Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

656 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