mysql.user=‘root’
mysql.password=‘XXX’
mysql.query('USE '+dataBaseName)
mysql.query(“set names ‘latin1’”)
mysql.query(“lock table tb1”)
mysql.query(“call proc1(p1,p2,@p3)”)
mysql.query(“unlock tables”)
mysql.end()
先安装mysql模块。
node.js默认安装时,模块文件放在 /usr/local/lib/node_modules 这个目录下,为了便宜管理,模块还是统一安装到这里好。
$ cd /usr/local/lib
$ npm install mysql
程序文件mysql.js
var Client = require('/usr/local/lib/node_modules/mysql').Client
var client = new Client()
client.user = 'root'
client.password = ''
console.log('Connecting to MySQL...')
client.query('USE tiny_shop') //如果MySQL中没有库表,赶紧建。
http = require("http")
var server = http.createServer(function(request, response) {
response.writeHeader(200, {"Content-Type": "text/html"})
client.query('SELECT * FROM tags', function selectCb(err, results, fields) {
if (err) {
throw err
}
var data = ''
for (var i=0i<results.lengthi++) {
var firstResult = results[i]
data += 'id: ' + firstResult['id']+'tag: ' + firstResult['tag']
}
response.write(data)
response.end()
})
})
server.listen(8080)
var sys = require("util")
sys.puts("Server running at http://localhost:8080/")
运行
$ node mysql.js
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)