获取列的值并在labelfield中显示

时间:2012-05-17 19:59:40

标签: sql sqlite blackberry

我想在labelfield中显示表格列的值(显示总金额)。

这是我用于开发标签的代码

  Border myBorder = BorderFactory.createBitmapBorder( 
    new XYEdges(20, 16, 27, 23),
    Bitmap.getBitmapResource("border.png"));
    LabelField myField = new LabelField("Total Amount Owed: Rs ",LabelField.USE_ALL_WIDTH | LabelField.FIELD_HCENTER)
    {
      protected void paint(Graphics g) {
        g.setColor(Color.BLUE);
        super.paint(g);
    }
    };  

   myField.setBorder(myBorder);  
   add(myField); 

这是我想用来从表中获取值的sql语句:

 Statement statementG56 = db.createStatement("SELECT owe FROM GTemp5");
                    statementG56.prepare();
                    statementG56.execute();

如何将select语句包含到我的labelfield中,以便获取的数量位于标签标题旁边。

1 个答案:

答案 0 :(得分:1)

这是我实施的方式。希望它能帮助未来的读者。

                 try
                  {
                  //Open or create the database
                    Database db = DatabaseFactory.openOrCreate("database1.db"); 

                    Statement statementGF55 = db.createStatement("CREATE TABLE IF NOT EXISTS GTemp5(owe INTEGER)");
                    statementGF55.prepare();
                    statementGF55.execute();       
                    statementGF55.close();

                    Statement statementGF56 = db.createStatement("SELECT owe FROM GTemp5");
                    statementGF56.prepare();
                    statementGF56.execute();

                   Cursor c = statementGF56.getCursor();

                  while(c.next()) 
                  {

                                      System.out.println("Inside while for fetching total owed value"); 
                                        Row r;
                                        r = c.getRow();

                                         Border myBorder = BorderFactory.createBitmapBorder( 
                                         new XYEdges(20, 16, 27, 23),
                                         Bitmap.getBitmapResource("border.png"));
                                         String pp = "" + r.getObject(0);
                                        LabelField myField = new LabelField("Total Amount: Rs    " +pp,LabelField.USE_ALL_WIDTH | LabelField.FIELD_HCENTER)
                                            {
                                                  protected void paint(Graphics g)
                                                   {
                                                     g.setColor(Color.RED);
                                                      super.paint(g);
                                                    }
                                               };  

                                   myField.setBorder(myBorder);  
                                   vfm.add(myField); 

                                    }     

                      }
                      catch(Exception e) 
                      {         
                        System.out.println( e.getMessage() );
                        e.printStackTrace();     
                      }