Node.js连接MYSQL
20 Apr 2012弄了一上午,犯了很幼稚的错误,浪费了4个小时,不过也不算浪费时间,学习新知识就这样,得自己慢慢摸索,摸索的过程不算浪费时间…
好吧,今天介绍Node.js连接Mysql。
CMD下运行npm install mysql,默认安装到node/node_modules下,如图:

注意:这只是安装了Node.js需要的Mysql模块,具体运行还需要本地安装Mysql!上午就被卡在这里4个小时,希望你能够看到这个提示…避免再浪费时间。
如果你能直接想到要安装Mysql,那就是我智商有点低下了…
好吧,现在开启你的本地Mysql服务,我直接用的WAMP搞定的,查看进程里是否有一个mysqlid.exe在运行。
然后去建表:

最后,开始写代码!
var sys=require('util');
console.log('Connecting to MYSQL...');
var client=require('mysql').createClient({
'host':'localhost',
'port':'3306',
'user':'root',
'password':''
});
var date=new Date();
var createtime=date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
console.log(createtime);
console.log('Connected to MYSQL automatically.');
ClientConnectionReady=function(client){
console.log('ClientConnectionReady.');
client.query('USE LazyBlog',function(error,results){
if(error){
console.log('ClientConnectionReady Error:'+error.message);
client.end();
return;
}
ClientReady(client);
});
}
ClientReady=function(client){
var values=['Hello!','Lazynight',createtime];
client.query('INSERT INTO posts SET title=?,content=?,createtime=?',values,
function(error,results){
if(error){
console.log('ClientReady Error:'+error.message);
client.end();
return;
}
console.log('Inserted:'+results.affectedRows+'row.');
console.log('Id inserted:'+results.insertId);
}
);
GetData(client);
}
GetData=function(client){
client.query(
'SELECT * FROM posts',
function selectZ(error,results,fields){
if(error){
console.log('GetData Error:'+error.message);
client.end();
return;
}
console.log('Results:');
console.log(results);
console.log('Field metadata:');
console.log('fields');
console.log(sys.inspect(results));
if(results.length>0){
var firstResult=results[0];
console.log('Title:'+firstResult['title']);
console.log('Content:'+firstResult['content']);
console.log('Createtime:'+firstResult['createtime']);
}
}
);
client.end();
console.log('Connection closed.');
}
ClientConnectionReady(client);
运行结果:

OK,自己折腾吧,祝玩得愉快。
转载请注明:于哲的博客 » Node.js连接MYSQL