[英]unknown database with mysql-connector in python
I'm new to dealing with databases.我是处理数据库的新手。 I created a simple database in phpmyadmin and joined it with another database.
我在 phpmyadmin 中创建了一个简单的数据库,并将它与另一个数据库连接起来。 Now I'm trying to access the database in python with the mysql-connector module, but python doesn't seem to be able to detect it.
现在我正在尝试使用 mysql-connector 模块访问 python 中的数据库,但是 python 似乎无法检测到它。 I've named and saved my database as the one below, but I have no idea if this even connects or can find it.
我已将我的数据库命名并保存为下面的数据库,但我不知道这是否可以连接或可以找到它。 I converted the DB to a .sql, but I don't know what to do with it.
我将数据库转换为 .sql,但我不知道如何处理它。
import mysql.connector
mydb = mysql.connector.connect(
host='localhost',
user='root',
password='',
database='python_sql',
)
Furthermore, I have practically no knowledge of PHP, so I wasn't able to write some fancy script as some do.此外,我实际上对 PHP 一无所知,所以我无法像某些人那样编写一些花哨的脚本。 I see in the tutorials people get through this step no problem even when they make the program in phpmyadmin, so I don't know whats wrong with my code.
我在教程中看到,即使他们在 phpmyadmin 中制作程序,人们通过这一步也没有问题,所以我不知道我的代码有什么问题。 When I run it, I get the following error.
当我运行它时,出现以下错误。
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mysql/connector/connection_cext.py", line 216, in _open_connection
self._cmysql.connect(**cnx_kwargs)
_mysql_connector.MySQLInterfaceError: Unknown database 'python_sql'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/johnminton/Documents/Python/Python database internal/mysql-db.py", line 7, in <module>
database='python_sql'
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mysql/connector/__init__.py", line 264, in connect
return CMySQLConnection(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mysql/connector/connection_cext.py", line 80, in __init__
self.connect(**kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mysql/connector/abstracts.py", line 966, in connect
self._open_connection()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mysql/connector/connection_cext.py", line 219, in _open_connection
sqlstate=exc.sqlstate)
mysql.connector.errors.ProgrammingError: 1049 (42000): Unknown database 'python_sql'
EDIT: when I remove the 'database=python_sql' I get no error, so it seems to connect fine to my localhost编辑:当我删除 'database=python_sql' 时,我没有收到任何错误,所以它似乎可以很好地连接到我的本地主机
in this code, you can handle most errors that happened because connect在此代码中,您可以处理由于连接而发生的大多数错误
from mysql.connector import errorcode
try:
myconn = mysql.connector.connect(
host='your host name ',
user='username',
passwd='Password to access',
database='DataBase Name')
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
myconn = mysql.connector.connect(
host='localhost',
user='root',
passwd='1234',
)
mycurs = myconn.cursor()
mycurs.execute("CREATE DATABASE ttt")
myconn = mysql.connector.connect(
host='localhost',
user='root',
passwd='1234',
database='ttt'
)
else:
print(err)
else:
myconn.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.