python - SQLAlchemy:从连接中获取数据库名称
问题描述
我想知道是否可以在连接后获取数据库名称。我知道由函数'create_engine'(此处)创建的引擎是可能的,我希望在连接后也有同样的可能性。
from sqlalchemy import create_engine, inspect
engine = create_engine('mysql+mysqldb://login:pass@localhost/MyDatabase')
print (engine.url.database) # print the dabase name with an engine
con = engine.connect()
我查看了检查器工具,但无法检索数据库名称,例如:
db_name = inspect(con.get_database_name() )
可能是不可能的。任何想法 ?
非常感谢!
解决方案
对于 MySQL,执行select DATABASE() as name_of_current_database
应该就足够了。对于 SQL Server,它将是select DB_NAME() as name_of_current_database
. 我不知道有任何固有的便携方式可以做到这一点,而不管后端如何。
推荐阅读
- python - 将 Django 部署到谷歌云平台
- python - 根据条件删除数据中的列
- c# - .net core 3.0 构造函数参数问题
- docker - 在 openshift 中部署 Windows docker 镜像
- dart - 将 Uint8List 传递给指针
在飞镖:ffi - java - Iterating an Arraylist of Arraylists using forEach and lambda expression
- python - PyError 在 Julia 中使用 PyCall
- sql - 在 BigQuery 中创建具有重复嵌套列的表语句
- r - 创建一个表格,其中包含特定格式的日期作为列
- javascript - 检查哨兵报告模式是否已经打开以防止多个弹出窗口