Querying database

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

Querying database

Postby SamB09 » Sun Jan 03, 2010 6:17 pm

I've got a class that creates a database and populates it which is fine , but im trying to run a query on that database from another class to display the results in a list view . Im having trouble getting my head round how to access the database from another class, should i just use the openorcreate method? I tried this but i had an error so assumed it isn't the way .

Thanks
SamB09
Developer
Developer
 
Posts: 39
Joined: Tue Oct 27, 2009 9:22 pm
Location: England

Top

Postby jwei512 » Sun Jan 03, 2010 11:08 pm

So what you probably want to do is create a ListActivity and in the onCreate method create a cursor/adapter. Something like this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. // retrieve the contents of your database using a cursor
  3.  
  4. Cursor cursor = getContentResolver().query(YourDatabaseName.CONTENT_URI, null, null, null, null);
  5.  
  6. startManagingCursor(cursor);
  7.  
  8. String[] columns = new String[]{ YourDatabaseName.FIELD1, YourDatabaseName.FIELD2 };
  9.  
  10. int[] to = new int[]{R.id.field1, R.id.field2};            
  11.  
  12. SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.your_layout, cursor, columns, to);
  13.  
  14. this.setListAdapter(mAdapter);
  15.  
  16.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Does this help?
- jwei

http://thinkandroid.wordpress.com
Examples, Code, and Tutorials
jwei512
Senior Developer
Senior Developer
 
Posts: 135
Joined: Mon Jul 27, 2009 9:35 pm
Location: Palo Alto, CA

Postby SamB09 » Sun Jan 03, 2010 11:22 pm

Hi , yea i think thats pretty much what i need before that i just need to send a query specifying exactly what i want i think i can use the rawdbquery method for that.

Thanks
SamB09
Developer
Developer
 
Posts: 39
Joined: Tue Oct 27, 2009 9:22 pm
Location: England

Postby jwei512 » Sun Jan 03, 2010 11:24 pm

Cool you may not even need to do a rawQuery, the contentResolver.query() method can handle WHERE clauses as well as ORDER BY clauses so depending on what you're trying to do, that might suffice.
- jwei

http://thinkandroid.wordpress.com
Examples, Code, and Tutorials
jwei512
Senior Developer
Senior Developer
 
Posts: 135
Joined: Mon Jul 27, 2009 9:35 pm
Location: Palo Alto, CA

Postby SamB09 » Sun Jan 03, 2010 11:38 pm

Wait , cant this method be used to access the data "openOrCreateDatabase()" the class im calling the db from is in the same project so maybe the above method can be used?
SamB09
Developer
Developer
 
Posts: 39
Joined: Tue Oct 27, 2009 9:22 pm
Location: England

Postby jwei512 » Sun Jan 03, 2010 11:52 pm

Yea I think both will work. I was just trying to provide an alternative way of querying your databases, something that might be easier? But again, depends on what you're trying to do!
- jwei

http://thinkandroid.wordpress.com
Examples, Code, and Tutorials
jwei512
Senior Developer
Senior Developer
 
Posts: 135
Joined: Mon Jul 27, 2009 9:35 pm
Location: Palo Alto, CA

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 8 guests