Priyanka Nadkarni
Priyanka Nadkarni

Reputation: 1

Access the existing Database from new application

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);
            }

Upvotes: 0

Views: 38

Answers (1)

vijay jain
vijay jain

Reputation: 21

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 :)

Upvotes: 2

Related Questions