views:

959

answers:

6

I want to access a MySQL database directly from Javascript in an HTML page in Firefox.

Does such a library exist?

To be very clear CGI+AJAX will not work

Some background: I want to create something like a GUI front end for a MySQL database (that's not what it is but it's close enough). I'm thinking about doing this as a local HTML page using JavaScript but for that to work I would need MySQL bindings for Javascript under Firefox. I already have a working prototype in under 100 LOC but it requires a web server and for reasons that are beyond this question, that won't work.

NOTE: both the DB and the JavaScript will be running locally and are not intended as a public page. In fact the HTML file will loaded as a file://// file. The only reason I'm using JavaScript is that it's the only available system for doing GUI stuff where I need it.

I'm willing to install plug ins, dll's windows dependent stuff or what not to make this work.


Edit: It looks like the answer is, "It can be done, but it's going to be painful". As one of my options is to just spew out all the data as files (ugly, and not to flexible but it would work) I think I'm not going to pursue this.

+4  A: 

Javascript lives inside the browser. It can make HTTP requests to the outside, but not really much more. So by design you wont be able to bind to a program running locally. If MySQL did expose an HTTP service, it might be possible, but that's not the case.

You might be able to find a plugin for Firefox that exposes a MySQL API to Javascript, but I dont know any such plugin.

If you dont need specifically MySQL, but just a database accessible from Javascript, have a look at Google Gears. It is a Firefox / IE plugin that exposes a SQLite database and a few other goodies.

If you give more informations on what you are trying to build, we might be able to give you better advices ...

Guillaume
I have a pile of data in MySQL so whatever I use I will need to be able to load it from there.
BCS
+1  A: 

What you need is a HTTP service that exposes the data you want to fetch with JavaScript.

A small AJAX oriented server side script (PHP, Perl, Ruby, whatever) that takes a few parameters and does the MySQL query, sending the data to the client in an HTTP-and-JavaScript friendly manner (for example as image/jpeg or JSON).

You won't be able to set up anything useful (a working, cross-browser solution) that makes MySQL available to JavaScript. JavaScript can do HTTP, and that's about it. Adapt on the server side.

Tomalak
actually, what you describe is exactly what I'm trying to get rid of. Also in my case, it will be useful if it only runs on my computer.
BCS
Okay, I see. Maybe your choice of technology (HTML + JavaScript) stands more in your way than you'd like. If you are willing to install dlls, plugins and windows-dependent stuff to make this work, you can just as well go ahead and write a .NET application, or a HTA.
Tomalak
I'm working on a .NET app. It will be the 3rd time I have EVER done GUI in anything other than HTML (and only about the 5th time even counting HTML)
BCS
Good luck. :-) Compared to what you can do with a JavaScript framework, you are going to write a lot more LOC. But you won't hit *such* a barrier anytime soon.
Tomalak
+2  A: 

Unfortunately you need a Server. Or if you know how to and are ready to be platform/browser locked, you could write a plug-in for your browser of choice (as far as I know there is no dll for Explorer so you'll need to write one yourself).

You might want to look into a small server that requires no setup. I modded Lua's Xavante server, for a very similar reason to yours, so it runs with no external dependencies, thus I can install/uninstall the application with a single copy/paste.

Robert Gould
That looks viable.
BCS
A: 

I can't give you complete answer but here are the general idea how you can do it with just mysql + internet explorer + javascript (untested):

=> in javascript you can call a window application by using

var myshell = new ActiveXObject( "WScript.shell" );
myshell.run( program names );

=> so the idea is to call mysql.exe with the sql statements stored in sql file, then capture and parse the output:

mysql.exe -h localhost -u root dbo < script.sql > output.txt

=> this idea doesn't come without challenges:

  • you need to modify the sql file before calling mysql.exe
  • you need to open and parse the output file

as i mentioned above i haven't tested anything so this whole idea may not even work ...

Lukman
A: 

Interesting question. But you sure lift a lot of barriers, selecting a language/environment with lot of voluntary limitations to limit access to the underlying system...

I like Robert's suggestion, Xavante is really lightweight.

Otherwise, I think a viable solution could be to use a Java applet with JDBC access. I think you would need to sign the applet, which shouldn't be a problem.

I searched java applet jdbc in Google and saw lot of promising titles, IBM gives the source code of such applet (for DB2 access but it should be easily adaptable).

[EDIT] There is another way, to wrap mysqllib.dll with an XPCOM DLL, as explained in Native code in javascript. No idea how to really do it, but perhaps it can get you started.

PhiLho
The barriers are a result of not wanting to switch to something else. If I switch, all these problems go away, as does JS, HTML and everything but the DB.
BCS
+2  A: 

Javascript can access MySQL...but generally only on the server. I've done it with Rhino, a java based javascript interpreter. Just included the MySQL driver, and its available. I imagine you could probably do this with an applet as well.

using Rhino, it would be something like this:

var DATABASE = {

    database: 'blog_development',
    host: 'localhost',
    username: 'dbuser',
    password: 'dbpass'

};

function ArticleModel(properties) {
  for (var p in properties) {
    this[p] = properties[p];
  }
}

ArticleModel.findAll = function() {
    var results = [];

    var jsConnectionObj = new Packages.MysqlConnection();
    c = jsConnectionObj.open(DATABASE.host,
                             DATABASE.database,
                             DATABASE.username,
                             DATABASE.password);

    if (c) {
      var s = c.createStatement();
      s.executeQuery("SELECT * FROM articles;");
      var rs = s.getResultSet();
      while (rs.next()) {
          results.push(new ArticleModel({
            id: rs.getInt("id"),
            title: rs.getString("title"),
            body: rs.getString("body")
          }));
      }
      rs.close();
      c.close();  
      return results;
    }

    throw new Error('could not connect to database');      
};