Install mysql plug-in
Npm install mysql@2.0.0-alpha7
Location after successful installation:
C: \ Users \ User Name \ node_modules \ mysql
How to view the readme. md File
Http://daringfireball.net/projects/markdown/dingus
Small mysql example
Var mysql = require ('mysql ');
Var TEST_DATABASE = 'nodejs _ db ';
Var TEST_TABLE = 'test ';
// Create a connection
Var client = mysql. createClient ({
User: 'root ',
Password: 'rainbow ',
});
// Create a database
Client. query ('create database' + TEST_DATABASE, function (err ){
If (err & err. number! = Mysql. ERROR_DB_CREATE_EXISTS ){
Throw err;
}
});
// The callback function is not specified. If an error occurs, the callback function is displayed as a client error.
Client. query ('use' + TEST_DATABASE );
// Create a table and insert data
Client. query (
'Create table' + TEST_TABLE +
'(Id INT (11) AUTO_INCREMENT,' +
'Name VARCHAR (255), '+
'Primary KEY (id ))'
);
Client. query (
'Insert INTO '+ TEST_TABLE + ''+
'Set name =? ',
['Nodejs1 ']
);
Var query = client. query (
'Insert INTO '+ TEST_TABLE + ''+
'Set name =? ',
['Nodejs2 ']
);
// Query and set the callback function
Client. query (
'Select * from' + TEST_TABLE,
Function selectCb (err, results, fields ){
If (err ){
Throw err;
}
Console. log (results );
Console. log (fields );
Client. end ();
}
);