How do I get the URI of my SQLite database in my Android app?
I开发者_运维知识库 have an Android app with a database called "myTestDB" with a table called "list_items". I want to use the Cursor getContentResolver().query() method to get a cursor to add to a SimpleCursorAdapter. The first argument of the query() method is a URI and I'm not sure what the URI should look like.
You do not use getContentResolver()
to access a SQLite database directly. You use getContentResolver()
when working with content providers. You call query()
on a SQLiteDatabase
object to get a Cursor
for use with SimpleCursorAdapter
.
A database doesn't have a Uri (other than the file where it's located). Data managed through a content provider has one. If you want to use the content resolver to perform a query, you need to write a content provider first.
If you don't want to do that, simply use SQLiteDatabase's query
function - it returns a Cursor that you can pass to the SimpleCursorAdapter.
it is fairly straightforward Method call looks like this
mDataBase.query(TABLE_NAME, columns, null, null, null, null, null);
where, TABLE_NAME = your SQLite table name and columns is a string array like
String columns[] = new String[] { "Column1" };
If you want all columns, you can pass null
for second argument.
Correction: As mentioned by a few others, there is no URI.
精彩评论