65

Is it possible to make chrome extension that interacts with sqlite database similarly as firefox extension? Could you give me some advise or link where is more info about developing chrome extension interacting with sqlite?

thank you

martin
  • 93,354
  • 25
  • 191
  • 226
xralf
  • 3,312
  • 45
  • 129
  • 200

4 Answers4

29

You can use Web SQL API which is an ordinary SQLite database in your browser and you can open/modify it like any other SQLite databases for example with Lita.

Chrome locates databases automatically according to domain names or extension id. A few months ago I posted on my blog short article on how to delete Chrome's database because when you're testing some functionality it's quite useful.

NullUserException
  • 83,810
  • 28
  • 209
  • 234
martin
  • 93,354
  • 25
  • 191
  • 226
  • I'd like to use the same sqlite database from firefox and chrome. Is it possible with Web SQL API? – xralf Jun 23 '11 at 12:54
  • 1
    Definitely not with Web SQL API. Maybe with NPAPI (http://code.google.com/chrome/extensions/npapi.html) but that would be very difficult. Why you need to share the same database? Maybe you could use some external database (like Google App Engine) and your extensions (for Chrome and Firefox) would communicatte with it... just an idea :). – martin Jun 23 '11 at 13:06
  • Would it be possible to setup filesystem-level links between the databases? It would be an interesting experiment to try. – technomalogical Jul 09 '12 at 16:30
25

You might be able to make use of sql.js.

sql.js is a port of SQLite to JavaScript, by compiling the SQLite C code with Emscripten. no C bindings or node-gyp compilation here.

<script src='js/sql.js'></script>
<script>
    //Create the database
    var db = new SQL.Database();
    // Run a query without reading the results
    db.run("CREATE TABLE test (col1, col2);");
    // Insert two rows: (1,111) and (2,222)
    db.run("INSERT INTO test VALUES (?,?), (?,?)", [1,111,2,222]);

    // Prepare a statement
    var stmt = db.prepare("SELECT * FROM test WHERE col1 BETWEEN $start AND $end");
    stmt.getAsObject({$start:1, $end:1}); // {col1:1, col2:111}

    // Bind new values
    stmt.bind({$start:1, $end:2});
    while(stmt.step()) { //
        var row = stmt.getAsObject();
        // [...] do something with the row of result
    }
</script>

sql.js is a single JavaScript file and is about 1.5MiB in size currently. While this could be a problem in a web-page, the size is probably acceptable for an extension.

sampathsris
  • 21,564
  • 12
  • 71
  • 98
  • 2
    it should be noted that this is the only offering so far that potentially connects to existing DBs – dandavis Feb 09 '17 at 21:25
13

Chrome supports WebDatabase API (which is powered by sqlite), but looks like W3C stopped its development.

serg
  • 109,619
  • 77
  • 317
  • 330
  • Can I connect from chrome to me sqlite database and make some select, insert, update, delete statements? – xralf Jun 01 '11 at 15:46
  • @xralf If you mean external sqlite installation then no. Chrome comes with built-in sqlite which you can use (create database, do selects etc) – serg Jun 01 '11 at 15:57
11

I'm not quite sure if you mean 'can i use sqlite (websql) in chrome' or 'can i use sqlite (websql) in firefox', so I'll answer both:

Note that WebSQL is not a full-access pipe into an .sqlite database. It's WebSQL. You will not be able to run some specific queries like VACUUM

It's awesome for Create / Read / Update / Delete though. I made a little library that helps with all the annoying nitty gritty like creating tables and querying and a provides a little ORM/ActiveRecord pattern with relations and all and a huge stack of examples that should get you started in no-time, you can check that here

Also, be aware that if you want to build a FireFox extension: Their extension format is about to change. Make sure you want to invest the time twice.

While the WebSQL spec has been deprecated for years, even now in 2017 still does not look like it will be be removed from Chrome for the foreseeable time. They are tracking usage statistics and there are still a large number of chrome extensions and websites out there in the real world implementing the spec.

SchizoDuckie
  • 9,353
  • 6
  • 33
  • 40