Retrieve data from DB

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Retrieve data from DB

Postby shank586 » Thu Aug 07, 2008 6:34 am

hi all,

I need any one of your help, my question is this "Tell me the way to retrieve data from DB to an Array?"

I used the following way to get data from DB but I get error

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.             String[] from=new String[]{myDB.query(true, MY_DATABASE_NAME, "name","num", "num", "name", null, null, "num")};
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


please any on help me out


Thanks in advance
with regards
Shank586
shank586
Junior Developer
Junior Developer
 
Posts: 13
Joined: Fri Jun 20, 2008 3:22 pm

Top

Postby borya » Fri Aug 08, 2008 10:23 pm

You could try using query to return a cursor, then go through the values of the cursor to put them into your array, or alternatively, just skip the array and use the cursor.
borya
Developer
Developer
 
Posts: 27
Joined: Wed Jul 02, 2008 7:50 pm
Location: California, USA

Re: Retrieve data from DB

Postby ych.tiger » Mon Aug 11, 2008 3:15 am

It's a example for querying ContactMethod information.
Cursor is better than Arrary.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public ContactMethodBean queryById(String _id) {
  2.  
  3.                 // init
  4.  
  5.                 String selection = People.ContactMethods.CONTENT_DIRECTORY + "." +
  6.  
  7.                         ContactMethods._ID + "=?";
  8.  
  9.                 String args[] = new String[] { _id };
  10.  
  11.                 // query
  12.  
  13.                 Cursor cursor = cr.query(Contacts.ContactMethods.CONTENT_URI,
  14.  
  15.                                 projection, selection, args, null);
  16.  
  17.                
  18.  
  19.                 ContactMethodBean cm = null;
  20.  
  21.                 if (cursor.next()) {
  22.  
  23.                         // convert cursor to bean
  24.  
  25.                         cm = new ContactMethodBean();
  26.  
  27.                         cm._id = cursor.getString(0);
  28.  
  29.                         cm.person = cursor.getString(1);
  30.  
  31.                         cm.type = cursor.getString(2);
  32.  
  33.                         cm.kind = cursor.getString(3);
  34.  
  35.                         cm.data = cursor.getString(4);
  36.  
  37.                         cm.aux_data = cursor.getString(5);
  38.  
  39.                         cm.label = cursor.getString(6);
  40.  
  41.                 }
  42.  
  43.                 return cm;
  44.  
  45.         }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
User avatar
ych.tiger
Junior Developer
Junior Developer
 
Posts: 14
Joined: Tue Jul 08, 2008 4:07 am
Location: China

Postby plusminus » Mon Aug 11, 2008 10:46 am

This is how I usually do it:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                Cursor c = myDB.query(...);
  2.  
  3.  
  4.  
  5.                /* Check if our result was valid. */
  6.  
  7.                if (c != null) {
  8.  
  9.                     /* Get the indices of the Columns we will need */
  10.  
  11.                     final int firstNameColumn = c.getColumnIndex("FirstName");
  12.  
  13.  
  14.  
  15.                     /* Check if at least one Result was returned. */
  16.  
  17.                     if (c.first()) {
  18.  
  19.                          int i = 0;
  20.  
  21.                          /* Loop through all Results */
  22.  
  23.                          do {
  24.  
  25.                               i++;
  26.  
  27.                               /* Retrieve the values of the Entry
  28.  
  29.                                * the Cursor is pointing to. */
  30.  
  31.                               String firstName = c.getString(firstNameColumn);
  32.  
  33.                              
  34.  
  35.                               /* Add current Entry to results. */
  36.  
  37.                               results.add("#" + i + ": " + firstName);
  38.  
  39.                          } while (c.next());
  40.  
  41.                     }
  42.  
  43.                }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 3 guests