是否有对 MySQL 和 SQLite 的 Orange 支持?

数据挖掘
2022-03-10 03:02:09

作为 Orange 工作流程的一部分,我希望能够直接访问 MySQL 和 SQLite 数据库。我看到支持 PostgreSQL 和(可能)

2个回答

为了更准确,我刚刚完成了 Python(版本 3.7)脚本的示例,该脚本包含在工作流中,以通过 ODBC 连接器从 MySQL 服务器中提取数据,如下所示:

from Orange.data import Table, Domain, ContinuousVariable
from pyodbc import connect

connecteur = connect(DSN='basepython') # "table1" from a MySQL server via ODBC connector.
curseur = connecteur.cursor() # Creation of the cursor for data swept.

# Execution of the SQL Query.
curseur.execute("SELECT id, champ1, champ2, champ3 FROM table1")

# All data of "table1" are saved in "donnees".
donnees = curseur.fetchall()

liste_globale = [] # Final list : It will contain the final data.
liste_intermediaire = [] # Temporary list : Only used for conversion.

# Whereas having tuples as list elements, you must convert them into lists included into the main list.
for partie in donnees:
    for i in range(len(partie)):
        liste_intermediaire.append(partie[i])
    liste_globale.append(liste_intermediaire)
    liste_intermediaire = []

# Creation of the data domain : Names of the variables seen in Orange WorkFlow.
identite = ContinuousVariable("Identification")
colonne1 = ContinuousVariable("Colonne1")
colonne2 = ContinuousVariable("Colonne2")
colonne3 = ContinuousVariable("Colonne3")

domaine = Domain([identite, colonne1, colonne2, colonne3])

# Final data
out_data = Table(domaine, liste_globale) # Data seen at the output of the Python_Script widget.
print(out_data) # This line is only used for debugging, you can comment it.

# Don't forget to close the connection at the end.
connecteur.close()

此示例与以下工作流程一起使用: 工作流示例

当然,如果你想同时连接 MySQL 服务器和 SQLite,你可以用两个连接器调整这个脚本,例如:

connecteur1 = connect(DSN="MySQL_ODBC_DSN")
connecteur2 = connect(DSN="SQLite_ODBC_DSN")

您可以使用这两个独立的连接器,如下所示:

curseur1 = connecteur1.cursor()
curseur2 = connecteur2.cursor()

curseur1.execute("SELECT * FROM mysql_table")
curseur2.execute("SELECT * FROM sqlite3_table WHERE id='2'")

donnees1 = curseur1.fetchall() # If there are several rows to save.
donnees2 = curseur2.fetchone() # If there is only one row to save.

最后,不要忘记关闭每个连接:

# Closing of the two opened database connectors.
connecteur1.close()
connecteur2.close()

我也想访问 MySQL 或 SQLite,但直接支持只有 PostgreSQL 和 MSSQL。

不过,我需要创建对 MySQL 服务器的访问权限,因此可以使用另一种方式。

实际上,可以在工作流中插入 Python 脚本,这样您就可以调用 pyodbc 模块(从您的操作系统直接访问您的 DSN)或 sqlite3 / pymysql 模块。