Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
ashish117
Posts: 40
Registered: ‎07-20-2010
My Device: Not Specified
Accepted Solution

display the contents of sqlite database

Hey

 

I have created a sqlite database and stored entries into the database but am stuck while trying to display the contents of the database on the screen.

 

I tried thi, but it does not work.

 

        try
        {
            Database d;
            URI myURI = URI.create("file:///SDCard/Databases/SQLite_Guide/" +
                                   "MyTestDatabase.db");
            d = DatabaseFactory.open(myURI, true);
           Statement st =d.createStatement("SELECT * FROM Data;");
            st.prepare();
            st.execute();
            st.close();
            d.close();
        }

 

Can anyone please tell me on how I should proceed ?

 

Thanks a lot.

 

 

Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: display the contents of sqlite database

You should execute query statement (statements like SELECT that return a value) using st.getCursor() instead of st.execute(). Then you can iterate through the result set. Check out the docs for the Statement and Cursor classes.




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Developer
ashish117
Posts: 40
Registered: ‎07-20-2010
My Device: Not Specified

Re: display the contents of sqlite database

so this is what i am trying now -


            Database d;
            Statement st;
            URI myURI = URI.create("file:///SDCard/Databases/SQLite_Guide/" +
                                   "MyTestDatabase.db");
            d = DatabaseFactory.open(myURI);
            st= d.createStatement("SELECT * FROM Data;");
            Cursor c = st.getCursor();
            Row r = c.getRow();
            while(c.next()!=false)
            {
                Row r1=c.getRow();
                vertical.add(new RichTextField(r1)); // this is where i get the error. i am trying to display the contents of the row over here
                c.next();
            }

 

but i always get this error -

 

: cannot find symbol
symbol  : constructor RichTextField(net.rim.device.api.database.Row)
location: class net.rim.device.api.ui.component.RichTextField
                vertical.add(new RichTextField(r1));
                             ^
1 error

 

 

Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: display the contents of sqlite database

Well, yes, because there is no such constructor for a RichTextField. For each row, you need to extract the column values, turn the values into whatever String you want to display, and then create the RichTextField with that String.




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Developer
kanna
Posts: 70
Registered: ‎01-02-2009
My Device: Not Specified

Re: display the contents of sqlite database

[ Edited ]

Row will contains all the columns in the table.

 

You can use the getString(int index) method to get the String of the particular column and then you can add it to the RichTextField.


Check the API reference page for more details  API Reference

---------------------
Best Regards,
Kannabiran.
---------------------
Don't forget to mark your post as solved if you got answer. And give kudos.
Developer
ashish117
Posts: 40
Registered: ‎07-20-2010
My Device: Not Specified

Re: display the contents of sqlite database

Thanks so much for the help. It looks like I get most of it. Here's the snippet of the code -

 

            d = DatabaseFactory.open(myURI);
            st= d.createStatement("SELECT * FROM Data;");
            st.prepare();
            st.execute();
            Cursor c = st.getCursor();
            Row r = c.getRow();


            while(c.next())
            {
                
                Row r1=c.getRow();
               String sss = r1.getString(1);
                add(new RichTextField(sss));

           }

 

The only trouble is that it does not display anything on the screen. How do I solve this problem ?

 

Thanks a lot.

 

Developer
kanna
Posts: 70
Registered: ‎01-02-2009
My Device: Not Specified

Re: display the contents of sqlite database

Remove the Row r = c.getRow(); statement outside the while loop and check.


Bcoz, there itself first row in the resultset is assigned to r. If the query returns only one record, the while loop will not executed.

Before ensure that the data is available in the table.

---------------------
Best Regards,
Kannabiran.
---------------------
Don't forget to mark your post as solved if you got answer. And give kudos.
Developer
ashish117
Posts: 40
Registered: ‎07-20-2010
My Device: Not Specified

Re: display the contents of sqlite database

Thanks a lot. It worked.

 

P.S - I also had to remove the line st.execute();

Contributor
vituvian
Posts: 17
Registered: ‎12-01-2010
My Device: Not Specified

Re: display the contents of sqlite database

hello ....i am a newbie..and  this is my very first post in the forum 

 

this is my requirement. ..

 

i have two tables  in my database namely  manufacturer and model 

 

this first list  displays a list of manufacturers  and on clicking on one of them i need to load a new screen that lists all the models made by that manufacturer . Upon selecting a manufacturer and a model , a new screen that displays the details of the model is pushed.

 

here are my issues

 

1) Can we populate a list directly from a database? If yes pls tell how ? 

2)  a bunch of rich text fields are listed one below the other 

how do i  push a new screen on tapping any of the rich text fields?

         how do i know which rich text field was tapped ?

 

thx

Contributor
ddgeek
Posts: 26
Registered: ‎11-30-2010
My Device: Not Specified

Re: display the contents of sqlite database

You can do like this.

 

    private ListField listInfo;
    private Vector listInfoVector = new Vector();
    
    private void loadInfo()
    {
            Statement st = db.createStatement("SELECT ID,model FROM dbInfo where ManufacturID= " + mID +"order by ID");
            st.prepare();
            Cursor c = st.getCursor();
            Row r;
            int i = 0;
            while (c.next())
            {
                r = c.getRow();
                i++;
                int id = r.getInteger(0);    
                Info info = new Info();
                info.setInfoID(id);
                LabelField modelLabel = new LabelField(model,Field.FIELD_LEFT);

                listLockerInfo.addElement(new Info(id, modelLabel));
            }
            st.close();
            db.close();
            listInfo= new ListField(listInfoVector .size()){  
                protected boolean navigationClick(int status, int time)
                {            
                    index = listInfo.getSelectedIndex();
                      fieldChangeNotify(0);
                      return true;
                }
            };
            listInfo.setCallback(this); // We manage the interaction!
            this.add(listInfo);
    }


    public void drawListRow(ListField list, Graphics g, int index, int y, int width) {
        Info infoToDraw = (Info) this.get(list, index);
        listInfo.setRowHeight(40);
        
        g.drawRoundRect(0, y, width, 40, 20, 20);
        int drawColor = Color.WHITE;
        g.setColor(drawColor);

        g.drawText("        " +infoToDraw.model , 0,y+9);
    }

    class Info {
        // In this example, to save accessors, the names are public
        public LabelField model ;
        public int InfoID;
        
        public int getInfoID() {
            return InfoID;
        }

        public void setInfoID(int infoID) {
            InfoID = infoID;
        }
        public Info()
        {
            
        }
        public Info(int INFOID, LabelField m) {
            InfoID = INFOID;
            model=m;
        }
    }

private ListField listInfo;
    private Vector listInfoVector = new Vector();
    
    private void loadInfo()
    {
            Statement st = db.createStatement("SELECT ID,model FROM dbInfo where ManufacturID= " + mID +"order by ID");
            st.prepare();
            Cursor c = st.getCursor();
            Row r;
            int i = 0;
            while (c.next())
            {
                r = c.getRow();
                i++;
                int id = r.getInteger(0);    
                Info info = new Info();
                info.setInfoID(id);
                LabelField modelLabel = new LabelField(model,Field.FIELD_LEFT);

                listLockerInfo.addElement(new Info(id, modelLabel));
            }
            st.close();
            db.close();
            listInfo= new ListField(listInfoVector .size()){  
                protected boolean navigationClick(int status, int time)
                {            
                    index = listInfo.getSelectedIndex();
                      fieldChangeNotify(0);
                      return true;
                }
            };
            listInfo.setCallback(this); // We manage the interaction!
            this.add(listInfo);
    }


    public void drawListRow(ListField list, Graphics g, int index, int y, int width) {
        Info infoToDraw = (Info) this.get(list, index);
        listInfo.setRowHeight(40);
        
        g.drawRoundRect(0, y, width, 40, 20, 20);
        int drawColor = Color.WHITE;
        g.setColor(drawColor);

        g.drawText("        " +infoToDraw.model , 0,y+9);
    }

    class Info {
        // In this example, to save accessors, the names are public
        public LabelField model ;
        public int InfoID;
        
        public int getInfoID() {
            return InfoID;
        }

        public void setInfoID(int infoID) {
            InfoID = infoID;
        }
        public Info()
        {
            
        }
        public Info(int INFOID, LabelField m) {
            InfoID = INFOID;
            model=m;
        }
    }