Nodejs operations MySQL database

https://github.com/mysqljs/mysql

 

  How MySql data do, in fact, writing or simply use nodejs operation,

     1. Start node in your project  npm install mysql --save

     2. The introduction of the code in your new project

      

Copy the code
// introduced database 
var = MySQL the require ( 'MySQL'); 

// achieve local link 
var = mysql.createConnection Connection ({ 
    Host: 'localhost', 
    User: 'YF', 
    password: '123456', 
    Database: 'YF' 
})
Copy the code

 

   The best will not conflict with root

 3. After it is CRUD, attach the code

       Inquire

 

     

Copy the code
// 查找
function select() {
    connection.connect(function (err) {
        if (err) {
            console.error('error connecting:' + err.stack)
        }
        console.log('connected as id ' + connection.threadId);
    })

    connection.query('SELECT * FROM demo', function (error, results, fields) {
        if (error) throw error;
        console.log('The solution is:', results);
    });
    connection.end();
}
Copy the code

     Add to

Copy the code
//添加
function add() {
    let post = {
        id: 1,
        name: 'Hello MySql',
        age: 20,
        time: Date.now(),
        temp: 'deom'
    };
    let query = connection.query("INSERT INTO demo SET ?", post, function (error, results, fields) {
        if (error) throw error;
    })
    console.log(query.sql); //INSERT INTO posts 'id'=1, 'title'='Hello MySQL'
}
Copy the code

  modify

Copy the code
//修改
function updeate() {
    connection.connect(function (err) {
        if (err) {
            console.error('error connecting:' + err.stack);
        }
        console.log('connected as id ' + connection.threadId);
    });

    connection.query('UPDATE demo SET name=?where id?', ['update', 1], function (error, results, fields) {
        if (error) throw error;
        console.log('changed:' + results.changeRows + 'rows');
    });

    connection.end();

}
Copy the code

  delete

 

Copy the code
//删除
function deletes() {
    connection.connect(function (err) {
        if (err) {
            console.error('error connecting:' + err.stack);
            return;
        }
        connection.query('DELETE FROM demo SET where id=?', [ 1], function (error, results, fields) {
            if (error) throw error;
            console.log('deleted:' + results.affectedRows + 'rows');
        });
        console.log('connected as id ' + connection.threadId);
        connection.end();

    });

}
Copy the code

 

  Is not that simple ah just add the method name and parameters corresponding to where you need it

 

  How MySql data do, in fact, writing or simply use nodejs operation,

     1. Start node in your project  npm install mysql --save

     2. The introduction of the code in your new project

      

Copy the code
// introduced database 
var = MySQL the require ( 'MySQL'); 

// achieve local link 
var = mysql.createConnection Connection ({ 
    Host: 'localhost', 
    User: 'YF', 
    password: '123456', 
    Database: 'YF' 
})
Copy the code

 

   The best will not conflict with root

 3. After it is CRUD, attach the code

       Inquire

 

     

Copy the code
// 查找
function select() {
    connection.connect(function (err) {
        if (err) {
            console.error('error connecting:' + err.stack)
        }
        console.log('connected as id ' + connection.threadId);
    })

    connection.query('SELECT * FROM demo', function (error, results, fields) {
        if (error) throw error;
        console.log('The solution is:', results);
    });
    connection.end();
}
Copy the code

     Add to

Copy the code
//添加
function add() {
    let post = {
        id: 1,
        name: 'Hello MySql',
        age: 20,
        time: Date.now(),
        temp: 'deom'
    };
    let query = connection.query("INSERT INTO demo SET ?", post, function (error, results, fields) {
        if (error) throw error;
    })
    console.log(query.sql); //INSERT INTO posts 'id'=1, 'title'='Hello MySQL'
}
Copy the code

  modify

Copy the code
//修改
function updeate() {
    connection.connect(function (err) {
        if (err) {
            console.error('error connecting:' + err.stack);
        }
        console.log('connected as id ' + connection.threadId);
    });

    connection.query('UPDATE demo SET name=?where id?', ['update', 1], function (error, results, fields) {
        if (error) throw error;
        console.log('changed:' + results.changeRows + 'rows');
    });

    connection.end();

}
Copy the code

  delete

 

Copy the code
//删除
function deletes() {
    connection.connect(function (err) {
        if (err) {
            console.error('error connecting:' + err.stack);
            return;
        }
        connection.query('DELETE FROM demo SET where id=?', [ 1], function (error, results, fields) {
            if (error) throw error;
            console.log('deleted:' + results.affectedRows + 'rows');
        });
        console.log('connected as id ' + connection.threadId);
        connection.end();

    });

}
Copy the code

 

  Is not that simple ah just add the method name and parameters corresponding to where you need it

 

Guess you like

Origin www.cnblogs.com/sexintercourse/p/11669029.html