处理黑莓中的特殊字符并将其存储到数据库中

时间:2013-06-29 09:28:02

标签: sqlite blackberry

我有网络服务,我正在从网络服务中获取数据并将数据存储到数据库中,但是我提供了异常,我已经检查过我的数据库是在设备上成功创建的并且我正确关闭我的数据库已成功打开 我认为这是我的网络服务数据中的特殊字符的问题,我在我的网络服务中有一些故事,并且包含一些特殊字符...,当我尝试将它们插入我的数据库时,它们给出了SQL LOGIC ERROR 尝试{                 conn =(StreamConnection)Connector.open(“http://tandonindia.com/story_new_xml?user=500”+“; deviceside = true”/ +“; interface = wifi” /);

            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();  
            docBuilderFactory.setIgnoringElementContentWhitespace(true);
            docBuilderFactory.setCoalescing(true);

            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();    

            docBuilder.isValidating();      

            is = conn.openInputStream();    
            doc = docBuilder.parse(is);     
            doc.getDocumentElement().normalize(); 
            System.out.println ("ROOT ELEMENT OF DOCUMENT IS=  " +  doc.getDocumentElement().getNodeName());
            //add(new LabelField(""+  doc.getDocumentElement().getNodeName()));

            NodeList nodeList = doc.getElementsByTagName("item");
            for (int i = 0; i < nodeList.getLength(); i++){
                //System.out.println(""+nodeList.getLength());

                Node node = nodeList.item(i);
                if(node.getNodeType() == Node.ELEMENT_NODE) {

                      Element element = (Element) node;
                      NodeList nodelist = element.getElementsByTagName("Week");
                      Element element1 = (Element) nodelist.item(0);
                      NodeList fstNm = element1.getChildNodes();
                      System.out.print("Week-- " + (fstNm.item(0)).getNodeValue().toString());

                      Element element2 = (Element) node;
                      NodeList nodelist1 = element2.getElementsByTagName("Year");
                      Element element3 = (Element) nodelist1.item(0);
                      NodeList fstNm1 = element3.getChildNodes();
                      System.out.println("\nYear-- "+(fstNm1.item(0)).getNodeValue().toString());


                      //For retriving text from node notification
                      Element element4 = (Element) node;
                      NodeList nodelist2 = element4.getElementsByTagName("subject");
                      Element element5 = (Element) nodelist2.item(0);
                      NodeList fstNm2 = element5.getChildNodes();
                      System.out.println("subject--"  + (fstNm2.item(0)).getNodeValue().toString());


                      //For retriving text from node Link
                      Element element15 = (Element) node;
                      NodeList nodelist21 = element15.getElementsByTagName("storypath");
                      Element element51 = (Element) nodelist21.item(0);
                      NodeList fstNm22 = element51.getChildNodes();
                      System.out.println("storypath--"  + (fstNm22.item(0)).getNodeValue().toString());

                      try {
                            db2 = DatabaseFactory.open(str);
                             db2.beginTransaction();
                             Statement statmnt2 = db2.createStatement("INSERT into tbstory(Week,Year,subject,storypath) VALUES("+"'"+(fstNm.item(0)).getNodeValue().toString()+"','"+(fstNm1.item(0)).getNodeValue().toString()+"','"+(fstNm2.item(0)).getNodeValue().toString()+"','"+(fstNm22.item(0)).getNodeValue().toString()+"')");
                             System.out.println("INSERTING STORYIESSSSSSSSSSSSSSSSS");

                             statmnt2.prepare();
                             statmnt2.execute();
                             statmnt2.close();

                             db2.commitTransaction();
                             db2.close();
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            db2.close();
                            System.out.println("1111222333"+e.toString());

                            e.printStackTrace();
                        }

                }

            }
            System.out.println("----------------");

        }catch(Exception e){

            System.out.println("error 1"+e.getMessage());
        }

我得到的错误如下 net.rim.device.api.database.DatabaseException:INSERT into tbstory(Week,Year,subject,story [0.0] path)VALUES('25','2013','PRIZE VEGETABLE!','Baba告诉这个故事Kodaikanal的学生。


  now this time i have very less content , again i got the error on INSERT query and how can i find that which character is giving me error, i'll post my exception while adding a small string in my Database.. can u tell me according to you which character is giving  error  or how can i figure out  which character is giving me error

net.rim.device.api.database.DatabaseException: INSERT into tbstory(storypath) VALUES('O[0.0] ne day, the Pandit came excitedly and told Rabidas, a cobbler, "My friend Today the queen is coming [0.0] to have the holy bath. I have been called to do the rit[0.0] uals; for today is a very special day, whoever has a dip in mother Ganga could emerge all pure. Do C[0.0] ome Don't miss this opportunity" Rabidas shook his head and said "Man changa to katooti me Ganga", p[0.0] ointing to his leather bag, (if mind is pure Ganga will[0.0]  be in my leather bag).
[0.0] 
[0.0]  
[0.0] 
[0.0] Angrily the Pandit went away muttering, "When will this fool understand the truth and try to earn Go[0.0] d's Grace?"  His life will be spent in the company of  this disgusting leather only'.
[0.0] 
[0.0]  
[0.0] 
[0.0] In the evening the Pandit came panting, "Rabi, What a miracle occurred today  Ganga Mayya Blessed ou[0.0] r queen with a golden bangle and told her she will get the second one when a person as pure and as d[0.0] evoted as she come to there to dip in the holy river. T[0.0] omorrow the whole city of Kashi will come and see who that lucky Blessed person is".
[0.0] 
[0.0]  
[0.0] 
[0.0] Rabi said "My dear friend It must be you only; for who can be more purer and devoted than you who ta[0.0] ke hundreds of people for holy bath every day?"
[0.0] 
[0.0]  
[0.0] 
[0.0] Pleased and puffed up with pride the Pandit said, "I know it must be me only. Tomorrow I will be the[0.0]  first person to take the holy dip".
[0.0] 
[0.0]  
[0.0] 
[0.0] Next day, the Pundit came late in the evening with an air of dejection over him. Rabi asked "So, my [0.0] friend did you get the second bangle?
[0.0] 
[0.0]  
[0.0] 
[0.0] Rabi closed his eyes and silently prayed "Oh mother, why don't you save this poor Pundit from this d[0.0] ilemma? I know you are everywhere and  manifest anything at any time. Then he put his hand into his [0.0] leather bag and Lo Wonder  of wonders A shinning gold b[0.0] angle emerged
[0.0] 
[0.0]  
[0.0] 
[0.0] The Pandit was spellbound at this great miracle. He bowed down to the cobbler in all humility.
[0.0] 
[0.0]  
[0.0] 
[0.0] "My dear, you have opened my eyes. All work is worship of God; Inner Purity is greater than Outer Pu[0.0] rity.'): SQL logic error or missing database

2 个答案:

答案 0 :(得分:1)

为避免格式化此类问题或SQL injection attacks,您不应将字符串直接嵌入到SQL语句中,而应始终使用参数:

Statement statmnt2 = db2.createStatement(
    "INSERT INTO tbstory(Week,Year,subject,storypath) VALUES(?,?,?,?)");
statmnt2.prepare();
statmnt2.bind(1, fstNm.item(0).getNodeValue().toString());
statmnt2.bind(2, fstNm1.item(0).getNodeValue().toString());
statmnt2.bind(3, fstNm2.item(0).getNodeValue().toString());
statmnt2.bind(4, fstNm22.item(0).getNodeValue().toString());
statmnt2.execute();
statmnt2.close();

答案 1 :(得分:1)

您正在测试哪种版本的BBOS?我发现当您尝试使用事务时,OS 5的早期版本会抛出“Sql logic error”异常。唯一的解决方法是切换到隐式事务 - 只需跳过对beginTransaction / commitTransaction的调用。这会影响5.0.0.0和5.0.0.400以及5.0.0.402,5.0.0.405,5.0.0.602之间的所有BBOS应用程序版本。