简体   繁体   English

使用 sqlalchemy 的 mariadb 的 Python 数据库连接

[英]Python Database connection for mariadb using sqlalchemy

I am new to python.我是python的新手。 I am trying to make a database connection using python to mariadb database which is hosted on my local network.我正在尝试使用 python 建立与本地网络上托管的 mariadb 数据库的数据库连接。 I am using sqlalchmemy to make a connection.我正在使用 sqlalchmemy 建立连接。 But facing some errors this is my code但是面对一些错误,这是我的代码

from sqlalchemy import create_engine
engine = create_engine('MariaDBDialect://username:password@host:port/databasename')

The error I am getting is我得到的错误是

`Can't load plugin: sqlalchemy.dialects:MariaDBDialect`

If anyone knows what I am doing wrong please let me know.如果有人知道我做错了什么,请告诉我。 Thanks in advance.提前致谢。

I was just confused by this topic and created a short blog post about connector strings .我只是对这个主题感到困惑,并创建了一篇关于连接器字符串的简短博客文章

The following solution works for Python 3:以下解决方案适用于 Python 3:

requirements要求

pip install SQLAlchemy
pip install PyMySQL

Code代码

import sqlalchemy

SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://user:password@host/dbname'

# Test if it works
engine = sqlalchemy.create_engine(SQLALCHEMY_DATABASE_URI, echo=True)
print(engine.table_names())

Yes, it really is "mysql" and not "mariadb".是的,它确实是“mysql”而不是“mariadb”。

Just use mysql+mysqldb instead of MariaDB engine, they work pretty much similar.只需使用 mysql+mysqldb 代替 MariaDB 引擎,它们的工作方式非常相似。

create_engine('mysql+mysqldb://username:password@host:port/databasename')

Update: You also should install mysql-python for python2更新:您还应该为 python2 安装 mysql-python

pip install mysql-python

Or mysqlclient for python3:或者python3的mysqlclient:

pip install mysqlclient

Starting from SQLAlchemy 1.4.0b1 you can use mariadb dialect and and native mariadb connector.从 SQLAlchemy 1.4.0b1 开始,您可以使用 mariadb 方言和本地 mariadb 连接器。 Use the following url scheme: 'mariadb+mariadbconnector://' .使用以下 url 方案: 'mariadb+mariadbconnector://'

Prerequisites:先决条件:

  • SQLAlchemy 1.4.0b1 could be installed through pip install --pre sqlalchemy SQLAlchemy 1.4.0b1 可以通过pip install --pre sqlalchemy 安装
  • MariaDB Connector/Python should be also installed.还应安装 MariaDB 连接器/Python。 It has its own system dependency, MariaDB Connector/C.它有自己的系统依赖项,MariaDB Connector/C。

As Stepan mentioned, the MariaDB dialect is now directly supported within SQLAlchemy, which allows you to use the official MariaDB Python connector.正如 Stepan 所提到的,SQLAlchemy 现在直接支持 MariaDB 方言,这允许您使用官方的 MariaDB Python 连接器。

For example:例如:

import sqlalchemy

# Define the MariaDB engine using MariaDB Connector/Python
engine = sqlalchemy.create_engine("mariadb+mariadbconnector://app_user:Password123!@127.0.0.1:3306/database_name")

For more details on this you can check out an official MariaDB blog post on the subject here -> https://mariadb.com/resources/blog/using-sqlalchemy-with-mariadb-connector-python-part-1/有关更多详细信息,您可以在此处查看有关该主题的官方 MariaDB 博客文章 -> https://mariadb.com/resources/blog/using-sqlalchemy-with-mariadb-connector-python-part-1/

And also a deeper dive into setting up object relationships in part 2 -> https://mariadb.com/resources/blog/using-sqlalchemy-with-mariadb-connector-python-part-2/以及在第 2 部分中更深入地设置对象关系 -> https://mariadb.com/resources/blog/using-sqlalchemy-with-mariadb-connector-python-part-2/

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

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