0

I have created database in external storage using a particular application and created tables using the same application.Now i want to access the data from that database from a new android application, So do i have to use content providers or else i can directly access the database without using content providers? Please help!

listView.setOnItemClickListener(new OnItemClickListener() { private static final int DATABASE_VERSION = 5; private static final String UID = "_id"; private static final String STEP = "Steps"; private static final String ImagePath = "Image_Path"; private static final String AudioPath = "Audio_Path"; @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub Message.message(KitchenData.this, "Click List Item Number"); SQLiteDatabase db = openOrCreateDatabase("/mnt/sdcard/Kitchen_Data.db", MODE_MULTI_PROCESS, null); try { String CREATE_TABLE = "CREATE Table " + li.get(position) + "("+ UID + " INTEGER PRIMARY KEY AUTOINCREMENT ," + STEP+ " VARCHAR(255) NOT NULL ,"+ImagePath+" VARCHAR(255) NOT NULL,"+AudioPath+" VARCHAR(255) NOT NULL); "; db.execSQL(CREATE_TABLE); pos=li.get(position); }catch(SQLException e) { Message.message(KitchenData.this,"Errors in creating table "+e); } 
3
  • what you have tried ?. first try anything. Commented Mar 31, 2015 at 5:53
  • i have edited the question . That is the code where i have created database and created tables using a particular application Commented Mar 31, 2015 at 5:59
  • If you are expect to share data between these apps Recommonded way is content providers. Commented Mar 31, 2015 at 8:00

1 Answer 1

2
private static String DB_NAME ="YourDbName";// Database name 

DB_NAME here is the name of your database. It is assumed that you have a copy of the database in the assets folder, so for example if your database name is mbDB, then the value of DB_NAME will be mbDB.

private static String DB_NAME ="ordersDB"; 

copy your database to your ASSETS folder in your program.

put the following code in your Dbhelper class and call it in your CreateDatabase function.

private void copyDataBase() throws IOException { InputStream mInput = mContext.getAssets().open(DB_NAME); String outFileName = DB_PATH + DB_NAME; OutputStream mOutput = new FileOutputStream(outFileName); byte[] mBuffer = new byte[1024]; int mLength; while ((mLength = mInput.read(mBuffer))>0) { mOutput.write(mBuffer, 0, mLength); } mOutput.flush(); mOutput.close(); mInput.close(); } 

Hope it is the answer you need :)

Sign up to request clarification or add additional context in comments.

4 Comments

So that means content providers not required ?
Should i copy the database created in assets folder od same application project or the new application project ?
copy the already created datatbase into the new application projects assets folder.
@PriyankaNadkarni Content providers are required only if you want to share data between two apps.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.