Accessing the database in another application using the content provider

Discussion in 'Application Development' started by sriranga, Apr 19, 2010.

  1. sriranga

    sriranga New Member
    15

    Jan 27, 2010
    2
    0
    15
    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.
     

    Advertisement
  2. justjimjpc

    justjimjpc Premium Member
    VIP Member
    653

    Nov 3, 2008
    10,543
    210
    653
    Washington DC, USA

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

    have move this to the developers forum section
     
  3. powell

    powell Well-Known Member
    78

    Aug 14, 2009
    204
    117
    78
    Programmer
    PL
    You have prepared a nice array of database fields:

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

    Add the columns as a parameter to the query:

    Code (Text):
    1. cur = cpc.query(myUri, columns, null,null,null);
     
  4. sriranga

    sriranga New Member
    15

    Jan 27, 2010
    2
    0
    15
    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.
     

Share This Page

Loading...