Node.js + MySQL CRUD操作

jopen 11年前

    var CRUD = {            //新增            _insert: function (client, insertSQLString, value) {                client.query(insertSQLString, value, function (error, results) {                    if (error) {                        console.log("ClientReady Error:" + error.message);                        client.end();                        return;                    } else {                        console.log("Inserted:" + results.affectedRows + " row.");                        console.log("Insert success...");                            }                });            },            //查询            _select: function (client, selectSQLString) {                client.query(selectSQLString, function (error, results, fields) {                    if (error) {                        console.log("GetData Error:" + error.message);                        client.end();                        return;                    }                            if (results.length > 0) {                        var firstResult, resultSet = '';                                for (var i = 0; i < results.length; i++) {                            firstResult = results[i];                            resultSet += "ID:" + firstResult["ID"] + ' Name:' + firstResult["Name"] + " PWD:" + firstResult["PWD"] + "\n";                        }                        console.log(resultSet);                    }                });            },            //更新            _update: function (client, updateSQLString) {                client.query(updateSQLString, function (error, results) {                    if (error) {                        console.log("ClientReady Error:" + error.message);                        client.end();                        return;                    }                            console.log("Update success...");                });            },            //删除            _delete: function (client, deleteSQLString) {                client.query(deleteSQLString, function (error, results) {                    if (error) {                        console.log("ClientReady Error:" + error.message);                        client.end();                        return;                    }                            console.log("Delete success...");                });            }        };                exports._insert = CRUD._insert;        exports._select = CRUD._select;        exports._update = CRUD._update;        exports._delete = CRUD._delete;  
使用方式如下:
    var http = require("http"),            mysql = require("mysql"),             CRUD = require("./CRUD.js");                    var client = mysql.createConnection({                user: "root", //数据库用户名                password: "", //数据库密码                database: "node" //数据库            });                    http.createServer(function (request, response) {                CRUD._insert(client, "insert into User set Name=?,PWD=?", ['3', '3']);                //CRUD._update(client, "update User set Name='22' where ID=1");                //CRUD._delete(client, "delete from User where ID=1");                CRUD._select(client, "select * from User");            }).listen(8080);