Solved

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

Posted on 2016-09-20
8
26 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 500 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
[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

 

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
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 if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

737 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