总结一下mysql语句在node.js中的各种写法,参考了npm网站 mysql模块 给的实例。
//1 db.query('select * from tuanshang_users where user_id < 10',function(err,results,fields){ //if(err) throw err; console.log( results ); if(!!results.length){ console.log('查询到'+ results.length +'条数据'); }else{ console.log('没有相关数据'); } }); //2 db.query('select * from tuanshang_users where user_id=?',[8],function(err,results,fields){ if(err) throw err; console.log( results ); }); //3 db.query({ sql: 'select * from tuanshang_users where user_id=? and username=?', values: [43,'ABCD'], timeout: 40000 },function(err,results,fields){ if(err) throw err; console.log( results ); }); //4 db.query({ sql: 'select * from tuanshang_users where user_id=? and username=?', timeout: 40000 },[44,'ABCDEF'],function(err,results,fields){ if(err) throw err; console.log( results ); }); //5 var userid = 44; var columns = ['username','password']; var sql = 'select ?? from ?? where user_id=?'; db.query(sql,[columns,'tuanshang_users',userid],function(err,results,fields){ if(err) throw err; console.log( results ); });
//1 var o = {username:'cnode', password:'123456'}; db.query('insert into tuanshang_users set ?', o, function(err,result){ console.log( result ); if(result){ console.log('插入成功'); console.log( result.insertId ); }else{ console.log('插入失败'); } }); //2 db.query('insert into tuanshang_users set ?',{username:'hello',password:'123456'}, function(err,result){ //if(err) throw err; if(result){ console.log('插入成功'); console.log( result ); }else{ console.log('插入失败'); } }); //3 db.query({ sql: 'insert into tuanshang_users set ?', values: {username:'kankan',password:'123456'}, timeout: 40000 },function(err,result){ //if(err) throw err; console.log( result ); if( !!result ){ console.log('插入成功'); console.log( result.insertId ); }else{ console.log('插入失败'); } }); //4 5 参考查询select
//1 db.query('update tuanshang_users set ? where user_id=?',[{username:'hello'},8],function(err,result){ //if(err) throw err; console.log(result); if(!!result && !!result.changedRows){ console.log('更新成功'); console.log( result.changedRows ); }else{ console.log('更新失败'); } }); //2 db.query('update tuanshang_users set ? where user_id=5',{username:'hello'},function(err,result){ //if(err) throw err; console.log(result); if(!!result && !!result.changedRows){ console.log('更新成功'); console.log( result.changedRows ); }else{ console.log('更新失败'); } }); //3 db.query({ sql: 'update tuanshang_users set ? where user_id=?', values: [{username:'hello'},8], timeout: 40000 },function(err,result){ //if(err) throw err; console.log( result ); if(!!result && !!result.changedRows){ console.log('更新成功'); console.log( result.changedRows ); }else{ console.log('更新失败'); } }); //4 5 参考查询select
//1 db.query('delete from tuanshang_users where username="hello"',function(err,result){ //if(err) throw err; console.log( result ); if(!!result && !!result.affectedRows){ console.log('删除成功'); console.log( result.affectedRows ); }else{ console.log('删除失败'); } }); //2 db.query('delete from tuanshang_users where user_id=?',[10],function(err,result){ //if(err) throw err; console.log( result ); if(!!result && !!result.affectedRows){ console.log('删除成功'); console.log( result.affectedRows ); }else{ console.log('删除失败'); } }); //3 db.query({ sql: 'delete from tuanshang_users where user_id=?', values: [51], timeout: 40000 },function(err,result){ //if(err) throw err; console.log( result ); if(!!result && !!result.affectedRows){ console.log('删除成功'); console.log( result.affectedRows ); }else{ console.log('删除失败'); } }); //4 5 参考查询select
查询操作,会返回一个结果数组。如果没有查询数据,结果数组为空,即数组长度为0。
{ fieldCount: 0, affectedRows: 0, insertId: 0, serverStatus: 2, warningCount: 0, message: '', protocol41: true, changedRows: 0 }
在插入操作后,结果对象中 insertId 返回插入的数据在表中id的位置。
在更新操作后,结果对象中 changedRows 返回更新了多少行(表中有几条数据中的字段值发生变化), affectedRows 返回满足where条件影响的行。
在删除操作后,结果对象中 affectedRows 返回删除了多少行。如果是0表示没有删除行或者没有满足删除条件的数据行。