zoukankan      html  css  js  c++  java
  • 关于字符串为空的判断条件

    第一篇随笔,正好今天解决了一个问题,立马记录下,希望是好习惯的开始。

    问题描述:读取一个记录SQL查询语句的XML文档,生成SQL查询字符串。

    XML文档如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <SubQuery>
      <DBName>SQLServer</DBName>
      <Select>*</Select>
      <From>t_product</From>
      <Where>ID=1</Where>
      <Order>
      </Order>
      <Group>
      </Group>
    </SubQuery

    希望得到的字符串结果为:select * from t_product  where ID=1

    先贴下我一开始编写的代码:

    public string XmlToSQL(XmlDocument doc)
        {
            string str_select, str_from, str_where, str_order, str_group;
            str_select = null;
            str_from = null;
            str_where = null;
            str_order = null;
            str_group = null;
            XmlNode node_select, node_from, node_where, node_order, node_group;
            XmlNode root = doc.DocumentElement;
            node_select = root.SelectSingleNode("Select");
            str_select = node_select.InnerText;
            node_from = root.SelectSingleNode("From");
            str_from = node_from.InnerText;
            node_where = root.SelectSingleNode("Where");
            str_where = node_where.InnerText;
            node_order = root.SelectSingleNode("Order");
            str_order = node_order.InnerText;
            node_group = root.SelectSingleNode("Group");
            str_group = node_group.InnerText;
            string str = "select " + str_select + " from " + str_from;
            if (str_where.Length !=null )
            {
                str += " " + " where " + str_where;
            }
            if (str_order.Length !=null)
            {
                str += " " + " order by "+str_order;
            }
            if (str_group.Length !=null)
            {
                str += " " + " group by " +str_group;
            }
            return str;
        }

    运行结果为:select * from t_product  where ID=1  order by   group by 

    不是预期结果。

    当时思考str_order和str_group两个字符串的结果应该为null,不清楚哪里出错。

    然后编写测试程序,测试发现,这两个字符串结果不为null。

    字符串为空,但不为null。这个点,我之前一直都是理解错误了。

    然后想到通过测试字符串的长度作为判断条件。

    于是修改代码如下:

    public string XmlToSQL(XmlDocument doc)
        {
            string str_select, str_from, str_where, str_order, str_group;
            str_select = null;
            str_from = null;
            str_where = null;
            str_order = null;
            str_group = null;
            XmlNode node_select, node_from, node_where, node_order, node_group;
            XmlNode root = doc.DocumentElement;
            node_select = root.SelectSingleNode("Select");
            str_select = node_select.InnerText;
            node_from = root.SelectSingleNode("From");
            str_from = node_from.InnerText;
            node_where = root.SelectSingleNode("Where");
            str_where = node_where.InnerText;
            node_order = root.SelectSingleNode("Order");
            str_order = node_order.InnerText;
            node_group = root.SelectSingleNode("Group");
            str_group = node_group.InnerText;
            string str = "select " + str_select + " from " + str_from;
            if (str_where.Length >0 )
            {
                str += " " + " where " + str_where;
            }
            if (str_order.Length > 0)
            {
                str += " " + " order by "+str_order;
            }
            if (str_group.Length > 0)
            {
                str += " " + " group by " +str_group;
            }
            return str;
        }

    结果正确。

    可以看出,改动幅度很小。

    实现了具体目标,但并不清楚自己所用方法是否为最佳。

    欢迎讨论。

  • 相关阅读:
    free
    wc
    awk
    wall
    sed
    Genymotion常见问题整合与解决方案
    Genymotion常见问题整合与解决方案
    java.net.MalformedURLException 异常
    java.net.MalformedURLException 异常
    异常:android.os.NetworkOnMainThreadException
  • 原文地址:https://www.cnblogs.com/trey/p/4682689.html
Copyright © 2011-2022 走看看