我正在尝试让 Taipy 核心与 Sqlite 一起使用,但没有成功。 Taipy 似乎无法连接。
有人有一个小的工作示例吗?
问候, 马库斯
我尝试过与例如联系以下设置:
- 没有用户名和密码
- db_name:taipy 以及 taipy.db
- db_engine:sqlite
- 使用 import sqlite3 和不使用 import sqlite3
sales_history_cfg = Config.configure_sql_data_node(
id="sales_history",
db_username="",
db_password="",
db_name="taipy",
db_engine="sqlite",
read_query="SELECT * from sales",
write_query_builder=write_query_builder
)
# Configuration of tasks
task_cfg = Config.configure_task("double",
double,
sales_history_cfg,
output_data_node_cfg)
# Configuration of the pipeline and scenario
pipeline_cfg = Config.configure_pipeline("my_pipeline", [task_cfg])
scenario_cfg = Config.configure_scenario("my_scenario", [pipeline_cfg])
# Run of the Core
tp.Core().run()
最佳答案
我假设您使用的是taipy版本2.1.3。这是今天最新的。
您面临的错误是应该在数据节点的配置中提供db_extra_args
。一个空字典应该可以工作。 db_extra_args={}
。
我注意到您的代码中存在其他 3 个(潜在)问题:
- 缺少分号“;”在 SQL 查询的末尾。
- 缺少路径参数。它应该指向包含 SQLite 数据库文件的文件夹。如果未提供路径,Taipy 将在根文件夹中搜索它。 (在我的示例中,我将数据库存储在文件“../data/sqlite/taipy.db”中)
- db_name 应包含文件扩展名。 (在我的示例中是“taipy.db”)
下面是我用来让您的代码在我的环境中运行的代码。我之前在数据库“../data/sqlite/taipy.db”中创建了一个销售表 (CREATE TABLE sales (date int, nb_sales int);
)。
from taipy import Config
import taipy as tp
import pandas as pd
def double(data):
return "WHATEVER"
def write_query_builder(data: pd.DataFrame):
insert_data = list(
data[["date", "nb_sales"]].itertuples(index=False, name=None))
return [
"DELETE FROM sales;",
("INSERT INTO sales VALUES (?, ?);", insert_data)
]
sales_cfg = Config.configure_sql_data_node(
id="sales_history",
db_username="",
db_password="",
db_extra_args={},
db_name="taipy.db",
db_engine="sqlite",
read_query="SELECT * from sales;",
write_query_builder=write_query_builder,
path="../data/sqlite/"
)
output_data_node_cfg = Config.configure_data_node(id="output")
# Configuration of tasks
task_cfg = Config.configure_task("double", double, sales_cfg, output_cfg)
# Configuration of the pipeline and scenario
pipeline_cfg = Config.configure_pipeline("my_pipeline", [task_cfg])
scenario_cfg = Config.configure_scenario("my_scenario", [pipeline_cfg])
# Run of the Core
tp.Core().run()
请注意,当不需要 db_extra_args
时,在下一个版本 2.2 中将不再需要提供空字典(目标发布日期为 2023 年 5 月)。还计划对 2.2 进行文档增强,特别是针对所有受支持的 db_engine(SQLite、mssql、MySQL 或 PostgreSQL)的更多示例。
关于python - Taipy core 与 Sqlite datanode 无法连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75648899/