Node-mysql additions and deletions to seal assembly

Source: Internet
Author: User
Tags findone

varUtil=require (' Util ');varMysql=require (' MySQL '); Const HOST= ' 127.0.0.1 '; Const USER= ' Root '; Const PASS= ' '; const prefix= ' Blog_ '; Const DATABASE= ' App_blog '; Const PORT=3306Const DB=Mysql.createpool ({host:host, User:user, Password:pass, Database:database, port:port});varFindone=function(Table,where,callback) {//find a bar;    //Whre is arr; [{id:1},{username:admin}];    var_where= "; if(Util.isobject (where)) {_where+ = ' WHERE ';  for(varKinchwhere) {_where+=k+ "= '" +where[k]+ "' and"; } _where=_where.slice (0,-4); }Else if(typeofwhere = = ' String ') {_where= ' WHERE ' +where; }   varSql= "SELECT * from" +prefix+table+ "+_where+ ' LIMIT 1 '; Console.log (SQL+ '-------------------------'); Db.query (SQL,function(err,data) {if(Err) {callback (Err,0); }Else{callback (err,data[0]); }   });}varselect=function(Table,callback) {//Find all;    varSql= "SELECT * from" +prefix+table;    Console.log (SQL); Db.query (Sql,callback);}varInsert =function(table,obj,callback) {//insert into table () VALUES ()    //{username: ' Guojikai ', Age: ' + ', Sex: ' 1 '}    varFields= "; varValues= ";  for(varKinchobj) { fields+=k+ ', '; Values=values+ "'" +obj[k]+ "',"} Fields=fields.slice (0,-1); Values=values.slice (0,-1); varSql= "INSERT into" +prefix+table+ ' (' +fields+ ') VALUES (' +values+ ') '; Console.log (SQL+ '--------------'); Db.query (Sql,callback);}/** Sets is Object, where is object;*/varUpdate=function(table,sets,where,callback) {var_sets= "; var_where= "; varKeys= "; varValues= ";  for(varKinchsets) {_sets+=k+ "= '" +sets[k]+ "',"; } _sets=_sets.slice (0,-1);  for(varK2inchwhere) {_where+=k+ "= '" +where[k2]+ "' and"; }    //Update table Set username= ' admin2 ', age= ' where id= ' 5 ';    varSql= "UPDATE" +prefix+table+ ' SET ' +_sets+ ' +_where; Db.query (Sql,callback);}varDel=function(table,where,callback) {var_where= ";  for(varK2inchwhere) {_where+=k+ "= '" +where[k2]+ "' and"; }     varSql= "DELETE from" +prefix+table+ "+_where;}//var sql_select= "select * from Blog_article";module.exports={db:db, Insert:insert, Select:select, Find:find, Del:del, update:update};

var util=require (' util '); var mysql=require (' MySQL '); const host= ' 127.0.0.1 '; const user= ' root '; const pass= '; const prefix= ' Blog_ '; const database= ' app_blog '; const port=3306const Db=mysql.createpool ({    host:host,    user:user,    password:pass,    database:database,    port:port}); var findone= function (Table,where,callback) {//Find a;   //Whre is arr; [{id:1},{username:admin}];    var _where= ';    if (Util.isobject (WHERE)) {        _where+= ' WHERE ';        for (var k in WHERE) {            _where+= K + "= '" +where[k]+ "and";       }              _where=_where. Slice (0,-4);   }else if (typeof where = = ' string ') {        _where= ' where ' +where;  &nbs P }   var sql= "select * from" +prefix+table+ "+_where+ ' LIMIT 1 ';   console.log (sql+ '-------------------------');   db.query (sql,function (err,data) {        if (err) {            callback (err,0);       }else{          &N Bsp Callback (Err,data[0]);       }   }); var select=function (Table,callback) {//Find all;    var sql= "select * from" +prefix+table;    Console.log (SQL);    db.query (sql,callback);} var insert =function (table,obj,callback) {   //insert into table () values ()    //{username: ' Guojikai ', Age: ' + ', Sex: ' 1 '}    var fields= ';    var values= ';    for (var k in obj) {        fields+=k+ ', ';        values=values+ ' ' "+obj[k]+" ', "   }    Fields=fields.slice (0,-1);    Values=values.slice (0,-1);    var sql= "INSERT into" +prefix+table+ ' (' +fields+ ') VALUES (' +values+ ') ';    CONSOLE.log (sql+ '--------------');    db.query (sql,callback);} /**  sets is object;  where is Object;*/var update=function (table,sets,where,callback) {    var _SETS = ';    var _where= ';    var keys= ';    var values= ';    for (var k in sets) {&NB Sp       _sets+=k+ "= '" +sets[k]+ "',";   }    _sets=_sets.slice (0,-1);    for ( var K2 in where) {        _where+=k+ "= '" +where[k2]+ "' and";   }   //update table Set username= ' admin2 ', age= ' in '   where id= "5";    var sql= "UPDATE" +prefix+table+ ' SET ' +_sets+ ' +_where;     Db.query (sql,callback);} var del=function (table,where,callback) {    var _where= ';    for (var K2 in where) {    &NBSP ;   _where+=k+ "= '" +where[k2]+ "' and";   }     var sql= "DELETE  from" +prefix+table+ ' & nbsp; ' +_where;} var sql_select= "SELECT *From Blog_article ";module.exports={    db:db,    insert:insert,    select:select,    find:find,    del:del,    Update:update};

Node-mysql additions and deletions to seal assembly

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.