3
回答
node.js怎么向mysql数据库插入数据,菜鸟求救
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   

这个是网上找的,但是运行报错!!TypeError: client.connect is not a function
有没有运行成功的给个demo呢?谢谢!!

var util = require('util');

var Client = require('/Users/Administrator/AppData/Roaming/npm/node_modules/mysql').Client;
var client = new Client();
  
client.user = 'root';
client.password = 'w123456';
  
console.log('Connecting to MySQL...');
  
client.connect(function(error, results) {
  if(error) {
    console.log('Connection Error: ' + error.message);
    return;
  }
  console.log('Connected to MySQL');
  ClientConnectionReady(client);
});
  
ClientConnectionReady = function(client)
{
    client.query('USE mvc1', function(error, results) {
        if(error) {
            console.log('ClientConnectionReady Error: ' + error.message);
            client.end();
            return;
        }
        ClientReady(client);
    });
};
  
ClientReady = function(client)
{
  var values = ['Chad', 'Lung', 'Hello World'];
  client.query('INSERT INTO MyTable SET firstname = ?, lastname = ? , message = ?', 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 MyTable',
    function selectCb(error, results, fields) {
      if (error) {
          console.log('GetData Error: ' + error.message);
          client.end();
          return;
      }
      // Uncomment these if you want lots of feedback
      //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('First Name: ' + firstResult['firstname']);
        console.log('Last Name: ' + firstResult['lastname']);
        console.log('Message: ' + firstResult['message']);
      }
  });
  
  client.end();
  console.log('Connection closed');
};

---------------------------------------------------------------------------------------------------------------
//var uname="王五";
//var pwd="123";
//var value0=["王五","123"];
//var sql="insert into u_user set name=?,password=?";add(sql,value0,callback)
//var sql='insert into u_user(name,password) values(uname,pwd)';
//var sql='insert into u_user(name,password) values('+uname+','+pwd+')';
var sql='select * from u_user';
//var sql='insert into u_user(name,password) values("张思","123")';
function d(){
add(sql,function(err,result){
   if(err){
   console.log('err');
   }else{
   console.log('yes');
   }
    })
}
d();

//console.log(pw);
//连接数据库
function add(sql,callback){
var mysql=require('/Users/Administrator/AppData/Roaming/npm/node_modules/mysql');
    var conn=mysql.createConnection({
  'host':'localhost',
  'user':'root',
  'password':'w123456',
    });
    conn.connect();
conn.query('use mvc1');
    conn.query(sql,function(err,result){
if(err){
return callback(err,err);
}else{
return callback(null,result);
}

    });
    conn.end();
}

查找什么的没问题,插入就有问题,sql语句换来换去没搞定
举报
goldenMoon
发帖于1年前 3回/405阅
共有3个答案 最后回答: 1年前
var sql='insert into u_user set name=?,passward=?';
var values=['王娜','4589'];


//连接数据库
var mysql=require('/Users/Administrator/AppData/Roaming/npm/node_modules/mysql');
var conn=mysql.createConnection({
'host':'localhost',
'user':'root',
'password':'w123456',
});
conn.connect();
conn.query('use mvc1');

conn.query(sql,values,function(err,result){
if(err){
console.log(err);
}else{
console.log(result.insertId);
}

});
conn.end();

--------复杂的----
//var util = require('util');
  
var mysql = require('/Users/Administrator/AppData/Roaming/npm/node_modules/mysql');
var conn=mysql.createConnection({
  'host':'localhost',
  'user':'root',
  'password':'w123456',
});
  
console.log('Connecting to MySQL...');
  
conn.connect(function(error, results) {
  if(error) {
    console.log('Connection Error: ' + error.message);
    return;
  }
  console.log('Connected to MySQL');
  ConnectionReady(conn);
});
  
ConnectionReady = function(conn)
{
    conn.query('USE mvc1', function(error, results) {
        if(error) {
            console.log('ConnectionReady Error: ' + error.message);
            conn.end();
            return;
        }
        ConnReady(conn);
    });
};
  
ConnReady = function(conn)
{
  var values = ['Chd1', 'Lung', 'Hello World'];
  conn.query('INSERT INTO MyTable SET firstname = ?, lastname = ? , message = ?', values,
    function(error, results) {
      if(error) {
        console.log("ConnReady Error: " + error.message);
        conn.end();
        return;
      }
      console.log('Inserted: ' + results.affectedRows + ' row.');
      console.log('Id inserted: ' + results.insertId);
    }
  );
  GetData(conn);
}
  
GetData = function(conn)
{
  conn.query(
    'SELECT * FROM MyTable',
    function selectCb(error, results, fields) {
      if (error) {
          console.log('GetData Error: ' + error.message);
          conn.end();
          return;
      }    
  
      if(results.length > 0)
      {
        var firstResult = results[0];
        console.log('First Name: ' + firstResult['firstname']);
        console.log('Last Name: ' + firstResult['lastname']);
        console.log('Message: ' + firstResult['message']);
      }
  });
  
  conn.end();
  console.log('Connection closed');
};

顶部