[Tutor] Connecting to a new mysql database

Alan Gauld alan.gauld at yahoo.co.uk
Mon Jul 5 04:04:58 EDT 2021


On 04/07/2021 21:42, arthomas152 at comcast.net wrote:
> This works:
> 
> import mysql.connector
> 
> mydb = mysql.connector.connect(
>     host='localhost',
>     user='root',
>     password='xxxxxxxxxxxxxxx',
>     database='sakila')
> mycursor = mydb.cursor()
> mycursor.execute("Show tables;")
> myresult = mycursor.fetchall()
> for x in myresult:
>     print(x)
> mydb.close()
> 
>  
> 
> But, when I try to open a new mysql database, I receive the message "unknown
> database." 

Its always best to show us the code that does NOT work.
And to include a full cut n paste of the error message.

For example, I'm not sure what you mean by "open a new database"
Do you mean you are trying to create a database from Python?
Or do you mean accessing a newly created database with no
data in it yet?

How is the database being created? What are you trying to do with it?
Real code that breaks will hopefully answer those questions.

> Mysql reads the database without a problem. Do I need to register
> my database somehow?

This suggests that the database is already created?
So what exactly are you doing with MySQL to "read" it?
Again the actual SQL commands you are using (and output?) would help.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list