在OSX Lion下的SilkJS上使用MySQL

时间:2012-06-27 00:13:32

标签: javascript mysql macos silkjs

我想在OSX Lion下使用来自SilkJS的MySQL,但无法找到让它工作的确切步骤。我按照这里的一般说明进行了操作:

  1. 执行SilkJS OSX Lion Quick Install
  2. Install MySQL
  3. 来自SilkJS REPL的
  4. Load MySQL
  5. 下载/配置:

    $ curl http://silkjs.org/install-osx.sh | sh
    $ sudo port install mysql5-server +mysql5
    $ sudo -u _mysql mysql_install_db5
    $ sudo port load mysql5-server
    

    启动SilkJS REPL,我跑了:

    $ silkjs 
    SilkJS> var MySQL = require('MySQL');
    undefined
    SilkJS> var SQL = new MySQL();
    undefined
    SilkJS> SQL.connect();
    interpreter line 19 (main):
     (object) :
    
    SilkJS> SQL.startTransaction();
    Caught Signal 11 for process: 77312
    

    如何在Lion下运行SilkJS的简单MySQL查询?

1 个答案:

答案 0 :(得分:1)

OSX Lion上的silkjs新网页安装:

mschwartz@dionysus:~/src$ curl http://silkjs.org/install-osx.sh | sh
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1666  100  1666    0     0   3553      0 --:--:-- --:--:-- --:--:-- 79333
Installing SilkJS in /usr/local/silkjs

You may be asked to enter your password.
This is so the /usr/local directory can be made writable for the install.

Password:
Downloading SilkJS
######################################################################## 100.0%
Installation complete.

You need to add /usr/local/bin to your PATH environment variable.
This can be done by adding this line to your .bashrc file:
export PATH=/usr/local/bin:$PATH

You can run SilkJS with the following command:
$ silkjs

You can run the SilkJS HTTP Server with the following command:
$ httpd-silk.js yourapp.js

For instructions on setting up a WWW site for httpd-silk.js, see
http://silkjs.org/http-server/

现在看它有效:

mschwartz@dionysus:~/src$ silkjs
SilkJS> console.dir(require('MySQL'));
undefined line 1 (eval):
 function () {
    this.queryCount = 0;
    this.handle = null;
}

undefined
SilkJS> 

注意console.dir()显示一个函数。其余的方法定义为函数(构造函数)的属性。所以你必须构造一个MySQL()对象:

SilkJS> var mysql = require('MySQL');
SilkJS> m = new mysql();
SilkJS> console.log(m.connect)
function (host, user, passwd, db) {
        if (!this.handle) {
            host = host || Config.mysql.host;
            user = user || Config.mysql.user;
            passwd = passwd !== undefined ? passwd : Config.mysql.passwd;
            db = db || Config.mysql.db;
            this.handle = mysql.connect(host, user, passwd, db);
        }
    }
undefined
SilkJS>

现在,为了连接,您必须传入host,user,passwd和db。或者,如果您在httpd环境中运行,则设置Config.mysql,如下所示:     Config.mysql = {       host:'localhost',//或运行MySQL服务器的其他主机       user:'someuser',//在MySQL服务器中进行身份验证的用户名       passwd:'what',// MySQL服务器中用户名的密码       db:'database_name“//要连接的MySQL服务器中的数据库名称     };

然后,HTTP服务器将创建一个可请求请求的全局SQL对象。这是自动的,您可以使用SQL.getDataRows(),SQL.getScalar()等。