Click here to Skip to main content
15,918,243 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So, I am newbie to Sql and database concepts and I wished to create a program which does the basic tasks done in Sql but without using actual commands..

But I do not know why but the show databases and show tables commands does not yield any output and If I try twice for the same in terminal it throws some error

also I am adding in there the output and the error which came when selected the show commands twice

EDIT - just edited the code here to show only SHOW DATABASES command

Thank you for your time

Here is the code

What I have tried:

import mysql.connector

mydb = mysql.connector.connect(
    host = "localhost",
    user = "root",
    password = "mypasswd",)

cursor = mydb.cursor()

cursor.execute("SHOW DATABASES")
Updated 16-Aug-22 5:57am
[no name] 8-Aug-22 14:16pm    
Too much going on. Create the database; wrap that up; then move to the next stage / step. Your "create database column" process could benefit from a "for loop" based on column count.
Richard MacCutchan 9-Aug-22 4:21am    
Your execution of "SHOW DATABASES" ignores the results so the cursor is left waiting for you to read (or discard) the output. But you should also take Gerry's advice and code and test each part individually. Writing the entire application in this way just makes it more difficult to find and fix problems.
no name Aug2022 16-Aug-22 11:22am    
Thank you so much for your answer. I have tried it on a test code, but it didn't make any difference. Although I easily get output on MySQL command line. Alongside even SHOW TABLES command does not seem to give any output ( maybe show command hates me ). I have updated the question and just kept the test code used to use SHOW DATABASE command.
Richard MacCutchan 16-Aug-22 11:39am    
I still do not know what you are doing, but the following code works fine and lists all the MySQL databases.
def list_databases(connection):
  mycursor = connection.cursor()

  mycursor.execute('SHOW DATABASES')

  databases = mycursor.fetchall()

  for db in databases:
no name Aug2022 16-Aug-22 11:53am    
Thanks a lot , I was doing a really silly mistake. Did not fetch the list of databases for the the output

1 solution

Answered only to remove from unanswered queue: solved by OP.
Share this answer
no name Aug2022 16-Aug-22 12:04pm    
ok I will accept this

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900