简体   繁体   English

SQLAlchemy 和 sql“使用数据库”命令

[英]SQLAlchemy and the sql 'Use Database' command

I am using sqlalchemy and the create_engine to connect to mysql, build a database and start populating with relevant data.我正在使用 sqlalchemy 和 create_engine 连接到 mysql,构建数据库并开始填充相关数据。

edit, to preface, the database in question needs to be first created.编辑,作为序言,需要首先创建有问题的数据库。 to do this I perform the following commands为此,我执行以下命令

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)

Following creation of the database, I try to perform a 'use' command but end up receiving the following error创建数据库后,我尝试执行“使用”命令但最终收到以下错误

line 3516, in _escape_identifier
    value = value.replace(self.escape_quote, self.escape_to_quote)

AttributeError: 'NoneType' object has no attribute 'replace'

I traced the error to a post which stated that this occurs when using the following command in python 3我将错误追溯到一个帖子,该帖子指出在 python 3 中使用以下命令时会发生这种情况

engine.execute("USE dbname")

What else needs to be included in the execute command to access the mysql database and not throw an error.执行命令中还需要包含什么才能访问 mysql 数据库并且不抛出错误。

You shouldn't use the USE command - instead you should specify the database you wish to connect to in the create_engine connection url - ie:您不应该使用USE命令 - 相反,您应该在create_engine连接 url 中指定您希望连接的数据库 - 即:

database_address = 'mysql+pymysql://{0}:{1}@{2}:{3}/{4}'
database_address = database_address.format('username',
                                           'password',
                                           'ip_address',
                                           'port',
                                           'database')
engine = create_engine(database_address, echo=False)

The create_engine docs are here: https://docs.sqlalchemy.org/en/13/core/engines.html#mysql create_engine文档在这里: https://docs.sqlalchemy.org/en/13/core/engines.html#mysql

Figured out what to do,想好了要做什么,

Following advise from Match, I looked into SQLAlchemy and it's ability to create schema.根据 Match 的建议,我调查了 SQLAlchemy 及其创建模式的能力。

Found the following code找到如下代码

from sqlalchemy import create_engine
from sqlalchemy_utils import database_exists, create_database

database_address = 'mysql+pymysql://{0}:{1}@{2}:{3}/{4}?charset=utf8mb4'
database_address = database_address.format('username','password','address','port','DB')
engine = create_engine(database_address, echo=False)

if not database_exists(self.engine.url):
        create_database(self.engine.url)

So creating an engine with the Schema name identified, I can use the utility database_exists to see if the database does exist, if not, then create using the create_database function.因此,创建一个具有标识的架构名称的引擎,我可以使用实用程序database_exists查看数据库是否存在,如果不存在,则使用create_database function 创建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM