6

I want to access a MySQL database directly from JavaScript code 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 database and the JavaScript code 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 plugins, 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 too flexible, but it would work) I think I'm not going to pursue this.

6
  • Strange question... yes. Bad question... no. So upvoting to counter the down vote Commented Nov 18, 2008 at 8:06
  • Have you looked into the possibility to write a HTA for use in Internet Explorer? You can use JavaScript + all unsafe COM objects there are, make ODBC calls, write to the hard disk, whatever. You just can't use Firefox anymore - but FWIW, you're tying yourself in quite a lot already. Commented Nov 18, 2008 at 8:53
  • Upvoted cause you made me go "Hum" Commented Nov 18, 2008 at 9:07
  • Oh, crud. Your kidding. The only thing I'm not willing to do (IE) is the only way I'm going to make it work? Commented Nov 18, 2008 at 18:47
  • 1
    NoSQL databases like CouchDB can communicate directly with Javascript as they have a REST based interface (as well as the documents, views and filters using JSON/Javascript). Commented Apr 8, 2010 at 10:15

9 Answers 9

4

JavaScript code lives inside the browser. It can make HTTP requests to the outside, but not really much more. So by design you won't 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 don't know any such plugin.

If you don't specifically need MySQL, but just a database accessible from JavaScript code, have a look at Google Gears. It is a Firefox / Internet Explorer plugin that exposes an SQLite database and a few other goodies.

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

Sign up to request clarification or add additional context in comments.

1 Comment

I have a pile of data in MySQL so whatever I use I will need to be able to load it from there.
2

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');      
};

Comments

2

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 Internet Explorer so you'll need to write one yourself).

You might want to look into a small server that requires no setup. I modified 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.

Comments

1

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.

4 Comments

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.
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.
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)
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.
0

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.

1 Comment

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.
0

Surely if javascript can make a call to a server it can make a call to the local ip address (192.168.x.x) and it can be handled using a program that listens on a specific port? All the program would have to do then is interact with the database, find the information and pass it back to the javascript?

1 Comment

The hope is (was?) to avoid needing that piece of glue code at the extra process it entails and rather have the JS talk directly to the MySQL server.
0

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 Windows 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 an 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 ...

Comments

0

If you use node.js, I think you might want to take a look at this. http://nodejsdb.org/

1 Comment

That would work server side (I think) but would require a bit more work client side.
0

I know this is an old thread, but I think it's still a relevant.

I've used backbone.js as a client lib in the browser. This simplifies (almost trivialises) data access from within the browser.

It depends on RESTful services on the server, and these can easily be provided. By way of example you could use node.js, phpwebsockets or socket.io

HTH

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.