How to use single database for multiple application?

Put your problem here if it does not fit any of the other categories.

How to use single database for multiple application?

Postby shishir.bobby » Fri Jul 17, 2009 6:27 am

i have three applications and i want to use a single database.
Which procedure should i follow ,to perform different actions like insert , update and delete , in a single database from different application .

Thanks in advance.

Stop using capslock for the topic!
shishir.bobby
Experienced Developer
Experienced Developer
 
Posts: 76
Joined: Thu Apr 09, 2009 11:41 am

Top

Postby nmc » Fri Jul 17, 2009 9:46 am

I would create the database on sd card; there its public accessable and your problem is solved.
nmc
Senior Developer
Senior Developer
 
Posts: 154
Joined: Thu Nov 27, 2008 8:30 pm
Location: Germany

Postby shishir.bobby » Fri Jul 17, 2009 10:58 am

can u show me,how can i create db on sd card.



thank for quick reply
shishir.bobby
Experienced Developer
Experienced Developer
 
Posts: 76
Joined: Thu Apr 09, 2009 11:41 am

Postby nmc » Fri Jul 17, 2009 11:24 am

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. SQLiteDatabase.openDatabase("/sdcard/demo.db3", null, SQLiteDatabase.OPEN_READWRITE + SQLiteDatabase.CREATE_IF_NECESSARY);
  3.  
  4.  
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


Depending on your needs you can either copy a template to sd (from your .apk), or create the db from scratch by using "create table if not exists" statements.
nmc
Senior Developer
Senior Developer
 
Posts: 154
Joined: Thu Nov 27, 2008 8:30 pm
Location: Germany

Postby shishir.bobby » Fri Jul 17, 2009 11:35 am

didnt get u....


please expand ur explanation.....thanks again
shishir.bobby
Experienced Developer
Experienced Developer
 
Posts: 76
Joined: Thu Apr 09, 2009 11:41 am

Postby nmc » Fri Jul 17, 2009 7:41 pm

Please be more specific with your questions.
I am not going to write essays here, nor will I implement the entire code for you 8)
nmc
Senior Developer
Senior Developer
 
Posts: 154
Joined: Thu Nov 27, 2008 8:30 pm
Location: Germany

Top

Postby shishir.bobby » Sat Jul 18, 2009 5:08 am

Actually , i dont know how to create databse in sd card. is there any permission issue with sd card.....thats only i was asking :D

sorry if i m not clear with my problem....but u dont hv to be that rude. :oops: .....if u dont wanna write its ok...i will find my way to overcome my problem.....


Thanks again for post... :lol: :lol:
hv a nice day :lol:
shishir.bobby
Experienced Developer
Experienced Developer
 
Posts: 76
Joined: Thu Apr 09, 2009 11:41 am

Postby nmc » Sat Jul 18, 2009 12:04 pm

I didnt mean to be rude...

Actually , i dont know how to create databse in sd card. is there any permission issue with sd card.....thats only i was asking


This creates a new (or opens an existing) database on sd:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. SQLiteDatabase.openDatabase("/sdcard/demo.db3", null, SQLiteDatabase.OPEN_READWRITE + SQLiteDatabase.CREATE_IF_NECESSARY);
  2.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

Your app doesnt need any permissions to do this.

The newly created database will be completely empty of course. so you have to create the needed tables using some 'create table if not exists' statements then.
nmc
Senior Developer
Senior Developer
 
Posts: 154
Joined: Thu Nov 27, 2008 8:30 pm
Location: Germany

Postby shishir.bobby » Sat Jul 18, 2009 12:08 pm

this was good



i'll try that...



thakns for being so calm,and posting the reply


thanks and regards
shishir.bobby
Experienced Developer
Experienced Developer
 
Posts: 76
Joined: Thu Apr 09, 2009 11:41 am

You can use Content Provider

Postby Theseus » Mon Jul 20, 2009 8:02 am

You can use Content Providers. They were builted for Multiple apps sharing the same database.
User avatar
Theseus
Junior Developer
Junior Developer
 
Posts: 16
Joined: Wed Jul 15, 2009 9:34 am
Location: China

Postby shishir.bobby » Tue Jul 21, 2009 6:22 am

hi there


i used content provider only my application...i wanna know how can content providers shares the common db for multiple application

in content providers we do something like this

private static final String uriString ="content://org.shishir.pp.PDatabase";
public static final Uri CONTENT_URI = Uri.parse(uriString);

right?

if in another application i use this same uriString...will it be the same db or another??????


thanks and regards
shishir.bobby
Experienced Developer
Experienced Developer
 
Posts: 76
Joined: Thu Apr 09, 2009 11:41 am

Postby TheSmile » Tue Jul 21, 2009 7:17 am

Hi shishir.bobby,

yes, using the same URI in another application is possible to share your data.
That's actually the architecture of android for sharing data. Same would be with contacts; If your app manipulates contacts i.e., you would use the shared data source provided under the URI

Code: Select all
content://contacts/people/#


where # would be the ID of the dataset to receive. Android takes care of mapping(translating) the given URI to a database layer, something like

Code: Select all
select * from contacts where ID = #


Hope that helps.
Whatever comes ... just keep on smiling :)

http://blog.thesmile.de
User avatar
TheSmile
Junior Developer
Junior Developer
 
Posts: 20
Joined: Fri Jul 17, 2009 2:22 pm
Location: Germany

Postby shishir.bobby » Tue Jul 21, 2009 7:41 am

thanks TheSmile


Well this works...i did the same...
in my 2 application i provided the same uriString.....but

when i navigate to file explorer to find the db...both application creates a new db,with the same name.

what should i do next????


thanks and regards
shishir.bobby
Experienced Developer
Experienced Developer
 
Posts: 76
Joined: Thu Apr 09, 2009 11:41 am

Postby TheSmile » Tue Jul 21, 2009 8:01 am

Mmm,

that sounds a bit odd. If there are two databases, the location is obviously not the same. Are you using the exact same URIs?
The AUTHORITY of the ContentProvider has to be the same, too.

Usually you would create an interface defining the URIs with static variables and on your second, third, ... application you would just use the interface vars to get the right URIs back.

Could you place some code here so we can have a look at it?
Whatever comes ... just keep on smiling :)

http://blog.thesmile.de
User avatar
TheSmile
Junior Developer
Junior Developer
 
Posts: 20
Joined: Fri Jul 17, 2009 2:22 pm
Location: Germany

Postby shishir.bobby » Tue Jul 21, 2009 8:19 am

here id the db code for 2 application......plz let me the suitable way to achive this..

application 1


public class PillMeterDatabase extends ContentProvider{
public static final String COL_PATIENTID = "patient_id";
public static final String COL_MEDICINENAME = "medicine_name";
public static final String COL_PILLAMOUNT = "pill_amount";

private static final String uriString ="/sdcard/mydbshishir.sqlite";
public static final Uri CONTENT_URI = Uri.parse(uriString);

private static final String TAG = "DBAdapter";
// ---Database name---
private static final String DATABASE_NAME = "pp";
// ---Table name---
private static final String DATABASE_TABLE = "ppal";
// ---Database version---
private static final int DATABASE_VERSION = 1;
// ---Table creation---



private DatabaseHelper databaseHelper;
private SQLiteDatabase sQLiteDatabase;

public PillMeterDatabase(Context context) {
databaseHelper = new DatabaseHelper(context);
}
//This constructor is used by ContentResolver
public PillMeterDatabase(){

}
// ---This opens the database---
public void open() throws SQLException {
sQLiteDatabase = databaseHelper.getWritableDatabase();
}
// ---Closes the database---
public void close() {
databaseHelper.close();
}


public long insert(String medicineName, int remainingPill) {
open();
ContentValues initialValues = new ContentValues();
initialValues.put(COL_MEDICINENAME, medicineName);
initialValues.put(COL_PILLAMOUNT, remainingPill);

long rowId = sQLiteDatabase.insert(DATABASE_TABLE, null, initialValues);
close();
return rowId;
}


@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
long rowId = ContentUris.parseId(uri);
open();
Cursor cursor = sQLiteDatabase.query(DATABASE_TABLE, new String[] {
COL_PATIENTID,COL_MEDICINENAME,COL_PILLAMOUNT},
COL_PATIENTID +" = " + rowId, null, null, null, null);
return cursor;
}


@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// TODO Auto-generated method stub
return 0;
}
@Override
public String getType(Uri uri) {
// TODO Auto-generated method stub
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean onCreate() {
databaseHelper = new DatabaseHelper(getContext());
return true;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// TODO Auto-generated method stub
return 0;
}
//Static Database helper inner class
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase sQLiteDbase) {
// sQLiteDbase.execSQL(DATABASE_CREATE);
}

@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub

}


}
}










app 2


public class PillMeterDatabase extends ContentProvider{
public static final String COL_PATIENTID = "patient_id";
public static final String COL_MEDICINENAME = "medicine_name";
public static final String COL_PILLAMOUNT = "pill_amount";

private static final String uriString ="/sdcard/mydbshishir.sqlite";
public static final Uri CONTENT_URI = Uri.parse(uriString);

private static final String TAG = "DBAdapter";
// ---Database name---
private static final String DATABASE_NAME = "pp";
// ---Table name---
private static final String DATABASE_TABLE = "ppal";
// ---Database version---
private static final int DATABASE_VERSION = 1;
// ---Table creation---
private static final String DATABASE_CREATE = "create table ppal ("
+ COL_PATIENTID + " integer primary key autoincrement,"
+ COL_MEDICINENAME + " text not null, "
+ COL_PILLAMOUNT + " text not null);";


private DatabaseHelper databaseHelper;
private SQLiteDatabase sQLiteDatabase;

public PillMeterDatabase(Context context) {
databaseHelper = new DatabaseHelper(context);
}
//This constructor is used by ContentResolver
public PillMeterDatabase(){

}
// ---This opens the database---
public void open() throws SQLException {
sQLiteDatabase = databaseHelper.getWritableDatabase();
}
// ---Closes the database---
public void close() {
databaseHelper.close();
}


public long insert(String medicineName, int remainingPill) {
open();
ContentValues initialValues = new ContentValues();
initialValues.put(COL_MEDICINENAME, medicineName);
initialValues.put(COL_PILLAMOUNT, remainingPill);

long rowId = sQLiteDatabase.insert(DATABASE_TABLE, null, initialValues);
close();
return rowId;
}


@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
long rowId = ContentUris.parseId(uri);
open();
Cursor cursor = sQLiteDatabase.query(DATABASE_TABLE, new String[] {
COL_PATIENTID,COL_MEDICINENAME,COL_PILLAMOUNT},
COL_PATIENTID +" = " + rowId, null, null, null, null);
return cursor;
}


@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// TODO Auto-generated method stub
return 0;
}
@Override
public String getType(Uri uri) {
// TODO Auto-generated method stub
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean onCreate() {
databaseHelper = new DatabaseHelper(getContext());
return true;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// TODO Auto-generated method stub
return 0;
}
//Static Database helper inner class
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase sQLiteDbase) {
sQLiteDbase.execSQL(DATABASE_CREATE);
}

@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub

}


}
}


w8ing for ur reply "thesmile" :cry:
have a nice day :D



thanks and regards
shishir.bobby
Experienced Developer
Experienced Developer
 
Posts: 76
Joined: Thu Apr 09, 2009 11:41 am

Top
Next

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 14 guests