PyScript无法打开数据库文件,但可以在Python中工作



在我的HTML文档中使用PyScript,我遇到了一个错误:

JsException(PythonError: Traceback (most recent call last): 
File "/lib/python3.10/site-packages/_pyodide/_base.py", line 429, 
in eval_code .run(globals, locals) 
File "/lib/python3.10/site-packages/_pyodide/_base.py", line 300, 
in run coroutine = eval(self.code, globals, locals) 
File "", line 3, 
in sqlite3.OperationalError: unable to open database file )

在Python中运行程序时,它可以工作。我给了需要的文件所有权限(chmod 777),这没有解决任何问题。

<!DOCTYPE html>
<html lang="">
<head>
<meta charset="utf-8">
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
<style>
#Ausgabe {
background-color: red;
}
</style>
<py-env>
-sqlite3
</py-env>
</head>
<body>
<main>
<textarea id="Ausgabe"> </textarea>
</main>
<py-script>
import sqlite3
#opening the database 
connecting= "home/marcel/Schreibtisch/Studium/JustForFun/Betrag.db"
connection = sqlite3.connect(connecting)
cursor = connection.cursor()
#executing the connection and printing each row in the db
sql = "SELECT * FROM personen "
cursor.execute(sql)
connection.commit()
for dsatz in cursor:
ausgab= dsatz[0] + " " + dsatz[1] + " " + dsatz[2]
pyscript.write("Ausgabe",ausgab)
connection.close()
</py-script>
</body>
</html>

Pyscript在浏览器中运行时具有与JavaScript相同的限制。没有本地文件访问,没有套接字,没有任何试图从浏览器沙箱中逃出来的东西。

在您的示例中,您试图直接访问位于本地文件系统上的sqlite3数据库文件。这在浏览器内运行的Pyscript或JavaScript中是不可能的。

找到了这个答案,它对我很有用。

来自:JULIEN'S DATA BLOG

PyScript中的文件和库

去年我在使用PyScript时遇到的一个主要问题是访问本地或远程文件有多困难。由于PyScript创建了自己的虚拟运行时,让我的脚本与"外部世界"交互变得比最初看起来更具挑战性。你可能会问,为什么现在要回到这个问题上来呢?我最近读到主配置标签的工作方式有了很大的改进,使得导入库更容易,并允许用户做一些奇特的事情,比如加载本地和远程文件。废话不多说,让我们看看PyScript的新标签现在是如何工作的:

由于[[fetch]]配置参数,访问本地文件变得更加容易:

<py-config>
[[fetch]]
files = ["./whatever_file_you_want_to_work_with.extension"]
</py-config>

相关内容

最新更新