Accessing the database in another application using the content provider


Last Updated:

  1. sriranga

    sriranga New Member This Topic's Starter

    Joined:
    Jan 27, 2010
    Messages:
    2
    Likes Received:
    0
    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

    Joined:
    Nov 3, 2008
    Messages:
    10,543
    Likes Received:
    210

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

    have move this to the developers forum section
     
  3. powell

    powell Well-Known Member

    Joined:
    Aug 14, 2009
    Messages:
    204
    Likes Received:
    117
    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 This Topic's Starter

    Joined:
    Jan 27, 2010
    Messages:
    2
    Likes Received:
    0
    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...