[Tutor] Fwd: Connecting to a new mysql database
Alan Gauld
learn2program at gmail.com
Mon Jul 5 20:05:52 EDT 2021
Forwarding to tutor.
Please use ReplyAll or ReplyList when responding to tutor messages.
Otherwse it just comes to me!
Alan g.
-------- Forwarded Message --------
When I run this code, the table names are returned.
> 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()
When I run this code, I receive error messages.
> import mysql.connector
>
> mydb = mysql.connector.connect(
> host='localhost',
> user='root',
> password='xxxxxxxxxxxxxxx',
> database='GloCultur')
> mycursor = mydb.cursor()
> mycursor.execute("Show tables;")
> myresult = mycursor.fetchall()
> for x in myresult:
> print(x)
> mydb.close()
Here are the results”
C:\Users\Arthur
Thomas\AppData\Local\Microsoft\WindowsApps\python3.9.exe"
D:/GloCulture/Glo_opensesame.py
Traceback (most recent call last):
File "D:\GloCulture\Glo_opensesame.py", line 3, in <module>
mydb = mysql.connector.connect(
File "C:\Users\Arthur
Thomas\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\mysql\connector\__init__.py",
line 273, in connect
return MySQLConnection(*args, **kwargs)
File "C:\Users\Arthur
Thomas\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\mysql\connector\connection.py",
line 107, in __init__
self.connect(**kwargs)
File "C:\Users\Arthur
Thomas\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\mysql\connector\abstracts.py",
line 1003, in connect
self._open_connection()
File "C:\Users\Arthur
Thomas\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\mysql\connector\connection.py",
line 352, in _open_connection
self._do_auth(self._user, self._password,
File "C:\Users\Arthur
Thomas\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\mysql\connector\connection.py",
line 219, in _do_auth
self._auth_switch_request(username, password)
File "C:\Users\Arthur
Thomas\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\mysql\connector\connection.py",
line 318, in _auth_switch_request
raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1049 (42000): Unknown database
'glocultur'
Process finished with exit code 1
Python is 3.9
MsSQL is 8.0.25
Windows 10
I built the GloCultur inside MySQL. It has 8 tables and each has 24
records. It is in its own directory.
Any ideas about what might be wrong or how to fix it?
Thanks,
Arthur
-----Original Message-----
From: Alan Gauld <alan.gauld at yahoo.co.uk> Sent: Monday, July 5, 2021 04:05
To: tutor at python.org
Subject: Re: [Tutor] Connecting to a new mysql database
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