简体   繁体   中英

What are different ways to use SQLite (on the server) with pure JavaScript (in the browser)?

What I'm trying to do is access an SQLite database, which is on a server, from Google Chrome, using JavaScript and HTML.

I've seen Google Gears, but it says it's deprecated because of HTML5. I'd like to avoid server-side programs, the only application doing work should be the browser. Is it possible?

If you are 100% sure that you need the data on server only (which is how most of the applications are built), then what you need is a 3-tier architecture - the client, the server and the database. First of all, stop trying to access the database from your web-page. Instead, write a server-side routine/function in your language of choice to access/manipulate the data. Now your problem reduces to invoking the server-side function from your web-page which can be done using a simple HTTP submission and a mechanism on the server-end to listen to that HTTP request, call the function and then send a response. If you are in the Java world, then Servlets can do this for you. There are similar solutions on most other platforms as well - Ruby, PHP, Perl, C#.

There is a sub-standard of HTML5 which is called WebSQL, this allows you to run access a SQL database hosted by the browser. All implementations of the standard used SQLLite as the backend service. The WebSQL standard has been deprecated due to lack of other SQL dialects, and storage systems in favour of indexedDB.

WebSQL is still supported in a very large number of systems including: Chrome, Safari, Safari Mobile, Opera, and Android browser

A tutorial on how to use it is here: http://www.html5rocks.com/en/tutorials/webdatabase/todo/

IndexedDB looks to be the future spec, but it is only supported in Firefox and Chrome at the moment. Check out another tutorial (using the same example in the previous link) http://www.html5rocks.com/en/tutorials/indexeddb/todo/ that shows you how to use IndexedDB.

If the data is on the server, then you can only write to it with server side programs.

There are ways for JavaScript in a browser to interact with SQL databases directly, but they are limited to databases stored in the browser, not the server, (and to really crazy stuff involving sockets and server side databases which are network accessible (which SQLite isn't)).

As far as I know, SQLite doesn't have any HTTP interface built into it. Thus you'll need something on the server that accepts HTTP messages (also known as AJAX requests) from your JavaScript, talks to SQLite, and returns the data, in a form that JavaScript can work with (eg JSON).

Pretty much any web framework in the world will do this, so you just need one in a language you understand that supports SQLite as a database backend.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM