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 wil
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:
mysql.exe
As I mentioned above I haven't tested anything, so this whole idea may not even work ...
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');
};
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?