HTML y JS: leer la base de datos local

I want to make some sort of website on a USB stick which opens in a browser. The HTML file should be able to read a (sql-)database in the same folder using javascript. It's a little application for me. These are my files:

USB stick:
-- start.html
-- database.sql

HTML5 offers "local storage" and it works perfectly. But with this method I can't access a local, already existing database on the stick. The browser creates a separate, emtpy database and saves it not on the stick. I don't need to write datasets.

Is there a different method to read databases? Thanks!

preguntado el 29 de julio de 12 a las 22:07

A website is not ideal for what you're trying to achieve. Consider building a desktop application instead -

'"A website is not ideal for what you're trying to achieve"' I would like to proclaim that statement The Eufemism Of The Day. -

4 Respuestas

Puedes usar sql.js, which is a SQLite converted to JavaScript. You'll have to provide your own import/export functions though and, naturally, you can't export to filesystem in JS, but since you only intend to read, that won't be a problem.

Respondido 29 Jul 12, 22:07

You are not going to be able to get the web browser to run a sql query. The best thing you could do is have it load data using ajax from the local file. It is not possible to save the updated data base to the local file system however.

You could have the javascript read comma separated data or the data could be stored in xml or json.

If you are wishing to save data however I believe a desktop application is needed

Respondido 29 Jul 12, 22:07

If it is just for reading, it is possible to just read and parse a file using javascript. But I wouldn't use an SQL file, but rather a file containing JSON instead. I don't think there are JavaScript libaries that can read SQL, and it won't be an easy job to write one.

Mind though, that even if you manage to find one, all of the file's contents will be read into memory, and that it is not possible to write the file.

Since you want a local running database application, I would very sincerely advise you not to use HTML and Javascript, but rather Java or any native language that can read the database and do some proper memory management. You may stick to HTML/JS for the GUI, if you want to, but you may find it just as easy to leave HTML out of it altogether.

What you want now, is very uncommon. Database applications tend to have a backend, and websites tend not to be run from a USB stick.

Respondido 29 Jul 12, 22:07

The problem is that local storage is local to the system the browser is running on because it's local to the browser not from where the files originated.

This means you'll have to deliver the data to the browser for it to store the data locally. Right now your data is located on your stick.

You have a couple of options: 1. Deliver the data to the browser to create your local storage database via javascript. 2. Use Rob W's answer and supply a portable browser (good idea Rob!)

I've made no assumptions how the data is stored in your .sql file. If it's actually SQL, you're out of luck. Localstorage is not a sql database.

Good Article on the basics of local storage in HTML5

Respondido 29 Jul 12, 22:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.