About
Pricing
Community
Teams
Start Free Trial
Log in
mickt
asked on
9/20/2016
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'"):
Python
8
2
Last Comment
mickt
8/22/2022 - Mon
SOLUTION
Dave Baldwin
9/20/2016
THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
mickt
9/20/2016
ASKER
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 Baldwin
9/20/2016
I believe you have to process 'SHOW TABLES' and similar queries just like a SELECT query because that is what they are.
mickt
9/20/2016
ASKER
Sorry, I'm not following, can you please clarify for me?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
mickt
9/20/2016
ASKER
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
ASKER CERTIFIED SOLUTION
mickt
9/20/2016
THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Dave Baldwin
9/20/2016
Glad you got it figured out. I had to get some sleep.
mickt
9/25/2016
ASKER
Answers question.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Multiple DB and all working.
Trying to do/not do something based on the existence of a table.