如何在Android应用程序中获取SQLite数据库的URI ?

时间:2021-02-09 07:27:47

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.

我有一个Android应用程序,它有一个名为“testmydb”的数据库和一个名为“list_items”的表。我想使用游标getContentResolver().query()方法获取要添加到SimpleCursorAdapter的游标。query()方法的第一个参数是URI,我不确定URI应该是什么样子。

3 个解决方案

#1


-1  

it is fairly straightforward Method call looks like this

很简单的方法调用是这样的

mDataBase.query(TABLE_NAME, columns, null, null, null, null, null);

mDataBase。查询(表名、列、空、空、空、空、空);

where, TABLE_NAME = your SQLite table name and columns is a string array like

TABLE_NAME = SQLite表名和列是一个字符串数组

String columns[] = new String[] { "Column1" };

字符串列[]=新字符串[]{"Column1"};

If you want all columns, you can pass null for second argument.

如果想要所有列,可以为第二个参数传递null。

Correction: As mentioned by a few others, there is no URI.

更正:正如其他一些人所提到的,没有URI。

#2


4  

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.

不使用getContentResolver()直接访问SQLite数据库。在使用内容提供程序时,使用getContentResolver()。您可以调用SQLiteDatabase对象上的query()来获取一个游标,以便与SimpleCursorAdapter一起使用。

#3


1  

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.

数据库没有Uri(除了它所在的文件)。通过内容提供程序管理的数据有一个。如果您希望使用内容解析器执行查询,您需要首先编写内容提供程序。

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.

如果您不想这样做,只需使用SQLiteDatabase的查询函数—它返回一个游标,您可以将其传递给SimpleCursorAdapter。

#1


-1  

it is fairly straightforward Method call looks like this

很简单的方法调用是这样的

mDataBase.query(TABLE_NAME, columns, null, null, null, null, null);

mDataBase。查询(表名、列、空、空、空、空、空);

where, TABLE_NAME = your SQLite table name and columns is a string array like

TABLE_NAME = SQLite表名和列是一个字符串数组

String columns[] = new String[] { "Column1" };

字符串列[]=新字符串[]{"Column1"};

If you want all columns, you can pass null for second argument.

如果想要所有列,可以为第二个参数传递null。

Correction: As mentioned by a few others, there is no URI.

更正:正如其他一些人所提到的,没有URI。

#2


4  

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.

不使用getContentResolver()直接访问SQLite数据库。在使用内容提供程序时,使用getContentResolver()。您可以调用SQLiteDatabase对象上的query()来获取一个游标,以便与SimpleCursorAdapter一起使用。

#3


1  

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.

数据库没有Uri(除了它所在的文件)。通过内容提供程序管理的数据有一个。如果您希望使用内容解析器执行查询,您需要首先编写内容提供程序。

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.

如果您不想这样做,只需使用SQLiteDatabase的查询函数—它返回一个游标,您可以将其传递给SimpleCursorAdapter。