admin管理员组文章数量:1241152
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.
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.
Share Improve this question edited Apr 12, 2011 at 14:25 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Nov 18, 2008 at 7:32 BCSBCS 78.6k71 gold badges195 silver badges298 bronze badges 6- Strange question... yes. Bad question... no. So upvoting to counter the down vote – Robert Gould 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. – Tomalak Commented Nov 18, 2008 at 8:53
- Upvoted cause you made me go "Hum" – UnkwnTech 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? – BCS Commented Nov 18, 2008 at 18:47
- 1 NoSQL databases like CouchDB can municate directly with Javascript as they have a REST based interface (as well as the documents, views and filters using JSON/Javascript). – Greg K Commented Apr 8, 2010 at 10:15
9 Answers
Reset to default 4JavaScript 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...
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');
};
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.
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.
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.
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?
I can't give you plete 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 e 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 ...
If you use node.js, I think you might want to take a look at this. http://nodejsdb/
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
本文标签: Are there JavaScript bindings for MySQLStack Overflow
版权声明:本文标题:Are there JavaScript bindings for MySQL? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740063890a2222734.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论