我正在使用sqlalchemy和create_引擎连接到mysql,构建一个数据库并开始填充相关数据
编辑,作为前言,首先需要创建有问题的数据库。为此,我执行以下命令
database_address = 'mysql+pymysql://{0}:{1}@{2}:{3}'
database_address = database_address.format('username',
'password',
'ip_address',
'port')
engine = create_engine(database_address, echo=False)
Database_Name = 'DatabaseName'
engine.execute(("Create Databse {0}").format(Database_Name)
创建数据库后,我尝试执行“use”命令,但最终收到以下错误
line 3516, in _escape_identifier
value = value.replace(self.escape_quote, self.escape_to_quote)
AttributeError: 'NoneType' object has no attribute 'replace'
我将此错误追溯到一篇文章,其中指出在Python3中使用以下命令时会发生这种情况
engine.execute("USE dbname")
execute命令中还需要包含哪些内容才能访问mysql数据库而不引发错误
您不应该使用
USE
命令,而是应该在create_engine
连接url中指定要连接的数据库,即:create_engine
文档在这里:https://docs.sqlalchemy.org/en/13/core/engines.html#mysql弄清楚该怎么办
根据Match的建议,我研究了SQLAlchemy及其创建模式的能力
找到以下代码
因此,创建一个具有标识的模式名称的引擎时,我可以使用实用程序
database_exists
查看数据库是否存在,如果不存在,则使用create_database
函数创建相关问题 更多 >
编程相关推荐