• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Accessing the database in another application using the content provider

sriranga

Lurker
Hi,

Can any one tell me how to access the database in one application to other while using the content provider.

Table creation was successful and i am able to access the data in that application.
Here i am sending my code snippest for accessing the table in another application.

String columns[] = new String[] {"_id","roll","name"};
Uri myUri = Uri.parse("content://com.example.mytable/table1");

ContentProviderClient cpc = getContentResolver().acquireContentProviderClient(myUri);
Cursor cur=null;
try {

cur = cpc.query(myUri, null, null,null,null);

} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(cur==null){
Toast.makeText(this, "There is no records to Dispaly", Toast.LENGTH_LONG).show();
}
else{
if (cur.moveToFirst()) {
String id = null;
String roll=null;
String userName = null;
do {
id = cur.getString(cur.getColumnIndex(TABLE_ID));
userName = cur.getString(cur.getColumnIndex(TABLE_NAME));
roll=cur.getString(cur.getColumnIndex(TABLE_ROLL));
Toast.makeText(this, id + " " +roll+" "+ userName, Toast.LENGTH_LONG).show();
} while (cur.moveToNext());

i am getting the cur object as null. I gave the permissions to the table in the xml file

permission android:name="com.example.mytable.Permission.WRITE" android:protectionLevel="normal" android:description="@string/hello" />
<permission android:name="com.example.mytable.Permission.READ" android:protectionLevel="normal" android:description="@string/hello" />


so can please suggest me to over come this problem.
 
Hi,

Can any one tell me how to access the database in one application to other while using the content provider.

Table creation was successful and i am able to access the data in that application.
Here i am sending my code snippest for accessing the table in another application.

String columns[] = new String[] {"_id","roll","name"};
Uri myUri = Uri.parse("content://com.example.mytable/table1");

ContentProviderClient cpc = getContentResolver().acquireContentProviderClient(myUri);
Cursor cur=null;
try {

cur = cpc.query(myUri, null, null,null,null);

} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(cur==null){
Toast.makeText(this, "There is no records to Dispaly", Toast.LENGTH_LONG).show();
}
else{
if (cur.moveToFirst()) {
String id = null;
String roll=null;
String userName = null;
do {
id = cur.getString(cur.getColumnIndex(TABLE_ID));
userName = cur.getString(cur.getColumnIndex(TABLE_NAME));
roll=cur.getString(cur.getColumnIndex(TABLE_ROLL));
Toast.makeText(this, id + " " +roll+" "+ userName, Toast.LENGTH_LONG).show();
} while (cur.moveToNext());

i am getting the cur object as null. I gave the permissions to the table in the xml file

permission android:name="com.example.mytable.Permission.WRITE" android:protectionLevel="normal" android:description="@string/hello" />
<permission android:name="com.example.mytable.Permission.READ" android:protectionLevel="normal" android:description="@string/hello" />


so can please suggest me to over come this problem.


Welcome to the AF forums and enjoy your visits !! :cool:

have move this to the developers forum section
 
You have prepared a nice array of database fields:

Code:
String columns[] = new String[] {"_id","roll","name"};
but apparently you forgot to use it ;)

Add the columns as a parameter to the query:

Code:
cur = cpc.query(myUri, columns, null,null,null);
 
Thank you powell for your reply.
Actually i mentioned it earlier due to some other problem i did some modification on my code.:)
But if i mentioned
cur = cpc.query(myUri, columns, null,null,null);


then also the cursor is not pointing to the database.
 
Back
Top Bottom