javascript HTML/JS 作为本地 SQLite 数据库的接口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10470240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 09:59:20  来源:igfitidea点击:

HTML/JS as interface to local SQLite database

javascripthtmlsqliteweb-sql

提问by Daniel Buckmaster

I'm writing a pretty simple database application, which I want to be locally stored (as opposed to looking up a remote database). I like HTML/Javascript for designing interfaces, and they're cross-platform (everybody has a browser!), so I'd really like to write a webpage as a frontend. No client/server interaction should be involved - I just want the users to be able to interact with the database using a browser, instead of a native program.

我正在编写一个非常简单的数据库应用程序,我希望将其存储在本地(而不是查找远程数据库)。我喜欢用 HTML/Javascript 来设计界面,而且它们是跨平台的(每个人都有一个浏览器!),所以我真的很想写一个网页作为前端。不应该涉及客户端/服务器交互 - 我只希望用户能够使用浏览器而不是本机程序与数据库交互。

However, the only way I can see to access databases from a browser is using something like WebSQL or IndexedDB. Both of these, however, abstract away the process of managing the database file itself, and store it away in user settings somewhere. I want to distribute the database file itself along with the app.

但是,我可以看到从浏览器访问数据库的唯一方法是使用 WebSQL 或 IndexedDB 之类的东西。然而,这两者都抽象了管理数据库文件本身的过程,并将其存储在用户设置的某个地方。我想将数据库文件本身与应用程序一起分发。

In short: is there a way to use HTML/Javascript to modify a local SQLite database file? Or is HTML not the tool I should be using for this sort of application?

简而言之:有没有办法使用 HTML/Javascript 来修改本地 SQLite 数据库文件?或者 HTML 不是我应该用于此类应用程序的工具?

EDIT:possibly relevant

编辑:可能相关

采纳答案by Daniel Buckmaster

This is what I've ended up doing:

这就是我最终做的:

As referred to here, you can use Python to create a local web server. Thistutorial gives a basic infrastructure for the server handler. I had to deal with some issues, possibly caused by Python 3 or by using Chrome to access my local page.

至于提到这里,你可以使用Python来创建一个本地Web服务器。教程提供了服务器处理程序的基本基础结构。我不得不处理一些问题,可能是由 Python 3 或使用 Chrome 访问我的本地页面引起的。

My GET handler function ended up looking like this:

我的 GET 处理函数最终看起来像这样:

def do_GET(self):
    try:
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.send_header('Access-Control-Allow-Origin', '*')
        self.end_headers()
        try:
            fn = GETHANDLERS[self.path[1:]]
            self.wfile.write(fn().encode("utf-8"))
        except KeyError:
            self.wfile.write(self.path.encode("utf-8"))
        return
    except:
        self.send_error(404, 'File Not Found: {0}'.format(self.path))

Where GETHANDLERSis a dictionary mapping URLs to functions - for example, if you visit http://localhost/my_func, the GETHANDLERS['my_func']function is called. If no function exists for the URL, the URL is just echoed back.

GETHANDLERS将 URL 映射到函数的字典在哪里- 例如,如果您访问http://localhost/my_funcGETHANDLERS['my_func']则调用该函数。如果该 URL 不存在任何函数,则该 URL 仅被回显。

I've implemented functions that manipulate a local SQLite database with the sqlite3module. Here's an example of the query to list everything in the Categoriestable:

我已经实现了使用sqlite3模块操作本地 SQLite 数据库的函数。以下是列出表中所有内容的查询示例Categories

import sqlite3

def get_categories():
    con = sqlite3.connect('my.db')
    c = con.cursor()
    c.execute('''SELECT * FROM Categories;''')
    return [cat[0] for cat in c.fetchall()]

GETHANDLERS["categories"] = get_categories

To use the local app, it's necessary for the user to install Python, then run the server script before opening the webpage. I think this is pretty acceptable!

要使用本地应用程序,用户需要安装 Python,然后在打开网页之前运行服务器脚本。我觉得这个还可以接受!

回答by Placinta Salaru Alin

If you really want just a client side application, you should really consider using HTML5 tehnologies(WebSQL, Indexed DB).A good reading is here: html5 doctor. You will need to export the database somewhere, or let the user export the database and when the user reloads the page the database should reconnect. example: export websql to a csv-file.

如果你真的只想要一个客户端应用程序,你真的应该考虑使用 HTML5 技术(WebSQL、索引数据库)。这里有一个很好的阅读:html5 doctor。您需要在某处导出数据库,或者让用户导出数据库,当用户重新加载页面时,数据库应该重新连接。示例:将 websql 导出到 csv 文件

回答by Penev

You could use the sqlite3 connector and connect to a local database file from a HTA Application.

您可以使用 sqlite3 连接器并从 HTA 应用程序连接到本地数据库文件。

Here is the connector (one has to register the dll as described on the page)

这是连接器(必须按照页面上的描述注册 dll)

https://www.assembla.com/spaces/litex/documents

https://www.assembla.com/spaces/litex/documents

Check the HTA Demo App in the zip file (DLL is in \litex\bin folder, hta is in the script folder..)

检查 zip 文件中的 HTA Demo App(DLL 在 \litex\bin 文件夹中,hta 在脚本文件夹中..)