npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

lifekit-mysqlhelper

v1.0.1

Published

lifekit-mysqlhelper

Downloads

14

Readme

#mysql数据库帮助类

在mysql基础上的一层封装类,对于简单的数据库操作(单表增删改查、sql文件)提供简便的方法

#方法及使用

Sqlparser工具类

说明:主要用于sql的拼接,可以根据传入某些特定参数拼接出一条完整的sql,并且提供了加载sql.xml文件并取出对应配置的sql的功能

方法:

(1)、初始化工具类

    
    var sqlparser = require("mysqlhelper").Sqlparser;

(2)、根据表名称查询数据库所有数据


    var sql = sqlparser.queryAll("tablename");
    //=>select * from tablename

(3)、根据主键查询数据


    var sql = sqlparser.queryWithKey("tablename","GUID_","g0001");
    //=>select * from tablename where GUID_='g0001'

(4)、根据表的某个字段查询值


    var sql = sqlparser.queryWithColumn("tablename","NAME","小杰");
    //=>select * from tablename where NAME='小杰'

(5)、根据多个字段组成的json对象查询符合条件的数据


    var columnobj = {'name':'小杰','age':18};
    var sql = sqlparser.queryWithColumns("tablename",columnobj);
    //=>select * from tablename where name='小杰' and age='18'

(6)、根据一个字符串组成的where条件查询符合条件的数据


    var where = "name='小杰' and age='18'";
    var sql = sqlparser.queryWithWhere("tablename",where);
    //=>select * from tablename where name='小杰' and age='18'

(7)、根据表名称插入单行数据


    var arr = ["G0001","小杰",18];
    var sql = sqlparser.insertRow("tablename",arr);
    //=>insert into tablename values('G0001','小杰','18');

(8)、根据表名称按字段插入一行数据


    var columns = ["GUID","NAME","AGE"];
    var values = ["G0001","小杰",18];
    var sql = sqlparser.insertColumnForArray("tablename",columns,values);
    //=>insert into tablename (GUID,NAME,AGE) values ("G0001","小杰",18)

(9)、根据表名称按json对象插入数据


    var json = {"GUID":"G0001","NAME":"小杰","AGE":18};
    var sql = sqlparser.insertColumnForJson(tablename,json);
    //=>insert into tablename (GUID,NAME,AGE) values ("G0001","小杰",18)

(10)、根据表名称和主键更新字段


    var setobjs = {"NAME":"小杰","AGE":18};
    var sql = sqlparser.updateObjForKey("tablename",setobjs,"GUID","G0001");
    //=>update tablename set NAME='小杰',AGE=18 where GUID='G0001'

(11)、根据表名称和主键更新字段


    var setcolumns=["AGE","SEX"];
    var setvalues=[18,"男"];
    var sql = sqlparser.updateArrayForKey("tablename",setcolumns,setvalues,"NAME","小杰");
    //=>update tablename set AGE='18',SEX='男' where NAME='小杰'

(12)、根据表名称与字段数组更新对应表的字段信息


    var setcolumns = ["AGE","SEX"];
    var setvalues = [18,"男"];
    var wherecolumns = ["GUID","NAME"];
    var wherevalues = ["G00001","小杰"];
    var sql = sqlparser.updateForArray("tablename",setcolumns,setvalues,wherecolumns,wherevalues);
    //=>update tablename set AGE='18',SEX='男' where GUID='G00001' and NAME='小杰'

(13)、根据表名称与字段对象更新对应表的字段信息


    var setobjs = {"AGE":18,"SEX":"男"};
    var whereobjs = {"GUID":"G00001","NAME":"小杰"};
    var sql = sqlparser.updateForJson("tablename",setobjs,whereobjs);
    //=>update tablename set AGE='18',SEX='男' where GUID='G00001' and NAME='小杰'

(14)、根据表名称和字段删除指定行


    var sql = sqlparser.delete("tablename","NAME","小杰");
    //=>delete from tablename where NAME='小杰'

(15)、根据表名称和主键删除行


    var sql = sqlparser.deleteForKey("tablename","GUID","G00001");
    //=>delete from tablename where GUID='G00001'

(16)、根据表名和字段数组删除指定数据


    var keys=["GUID","NAME"];
    var key_values = ["G00001","小杰"];
    var sql = sqlparser.deleteForArray("tablename",keys,key_values);
    //=>delete from tablename where GUID='G00001' and NAME='小杰'

(17)、根据表名和字段对象删除指定数据


    var columnObjs = {"GUID":"G00001","NAME":"小杰"};
    var sql = sqlparser.deleteForJson("tablename",columnObjs);
    //=>delete from tablename where GUID='G00001' and NAME='小杰'

(18)、删除表的所有数据


    var sql = sqlparser.deleteAll("tablename");
    //=>delete from tablename

(19)、加载数据文件,根据sqlId取出对应的sql(这里需要注意,这个方法是异步方法,区别于原物联网平台)


    sqlparser.loadSqlFile(__filename,function(sqlProperty){
        var sqlId='C60B9A90B8154AF3863C7E2CCED0FF68', 
        mapkeys = new map(), 
        parms = [];
        var sql = sqlparser.getSql(sqlProperty,sqlId,mapkeys,parms);
    });
     

对应的xml文件同原物联网平台相同,例子:


    <?xml version="1.0" encoding="UTF-8"?>
    <!-- 报表管理模块对应sql -->
    <!-- action(操作类别):SELECT\INSERT\UPDATE\DELETE -->
    <!-- database(数据库级别):SYSTEM(主数据库)\PROJECT(从数据库) -->
    
    
    <!-- dbtype(数据库类别):ORACLE\MYSQL\SQLSERVER -->
    <Sqls>
        <Sql id="0F3812D6-5A57-495D-9560-B60CC358AC83" action="SELECT" database="SYSTEM" dbtype="MYSQL" desc="获取区域下对应的站点">
            <![CDATA[SELECT GUID_, NAME_, CODE_, REF_CODE FROM #{database}.TB_BASE_STATION WHERE PORTAL_ID='?' AND AREA_CODE LIKE '?%' AND CODE_='?']]>
        </Sql>
    </Sqls>

##dbhelper工具类

说明:主要提供执行SQL的方法,自行管理数据库连接,此类提供一个全局变量dbhelper

方法:

(1)、初始化工具类

    
    require("mysqlhelper").dbhelper("iot","192.168.1.212",3306,"iot","iot");

初始化之后就会有一个全局变量global.dbhelper产生

(2)、根据传入的sql查询结果集

    
    var sql = "select * from table";
    global.dbhelper.execDataSet(sql,function(err,rows){
        
    });

(3)、根据传入的sql操作数据库(增删改)


    var sql = "update table set name='小杰' where guid='ga001'";
    global.dbhelper.execNonQuery(sql,function(err,rows){
        
    });