Are there JavaScript bindings for MySQL?

后端 未结 9 1910
礼貌的吻别
礼貌的吻别 2020-12-17 00:56

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

相关标签:
9条回答
  • 2020-12-17 01:46

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

    0 讨论(0)
  • 2020-12-17 01:57

    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');      
    };
    
    0 讨论(0)
  • 2020-12-17 01:57

    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?

    0 讨论(0)
提交回复
热议问题