0

Hi i used this coding to create table and insert the data into the table. and i used another application to get the value from the same table it shows no such table found please anyone can help..

    try{
                   Toast.makeText(FiveActivity.this, "In try ",Toast.LENGTH_LONG).show();   
                   db = openOrCreateDatabase( "Emergency.db", SQLiteDatabase.CREATE_IF_NECESSARY, null );

                   String CREATE_CONTACTS_TABLE = "CREATE TABLE if not exists userinformation (mail VARCHAR(255),pass VARCHAR(255))";
                   db.execSQL(CREATE_CONTACTS_TABLE);

                   String qu="select * from userinformation";
                   c=db.rawQuery(qu,null);

                   int count = c.getCount();

                   String[] values = new String[count + 1];
                   int i = 0;
                   for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
                       values[i] = c.getString(c.getColumnIndex("mail"));
                       Toast.makeText(FiveActivity.this, values[i],Toast.LENGTH_LONG).show();
                       i++;
                    }  


                   i=0;
                   if(count==0)
                   {
                   String    Query =    "insert into userinformation (mail,pass) values ('"+ mailuser+ "','" + password + "')";
                   db.execSQL(Query);
                   Toast.makeText(FiveActivity.this,"Values Inserted",Toast.LENGTH_LONG).show();
                   }
Happy
  • 1,031
  • 10
  • 26
  • sorry you can not access database from other application because its databases are stored in application storage under package directory. – Biraj Zalavadia Feb 06 '14 at 10:25
  • [link](http://stackoverflow.com/questions/7053809/share-sqlite-database-between-2-android-apps) Other options 1. ContentProviders 2. IPC – gvmani Feb 06 '14 at 10:36

3 Answers3

0

Databases are specific to the application. When you access a database according to its name (like you did here with "Emergency.db"), the DB file is actually created within the application's "App Data" storage space. This means that this DB is hidden to other applications.

If you want to pass data between apps, use the usual methods.

PaF
  • 3,297
  • 1
  • 14
  • 15
0

Why do you want to do that? If you want to get anything from the other app, use intents. Request(with startActivityForResult) the other app to give anything, as a result other app would respond and give a result(with setResult). And use that result in onActivityResult. I would have chosen this kind of approach.

Seshu Vinay
  • 13,560
  • 9
  • 60
  • 109
0

sorry you can not access database from other application because its databases are stored in application storage under package directory.

If you want to access database from multiple application you need to create database on sdcard. Follow the stpes.

First you have to specify the path of the sdcard. You can do that by creating a string like this:

public static final String  DATABASE_FILE_PATH = "/sdcard";

But for you should call

Environment.getExternalStorageDirectory()   

to get the root path to the SD card and use that to create the database. After that you create the database as you want. Here is an example

public class DatabaseHelper {
    private static final String TAG = "DatabaseHelper";
    public static final String DATABASE_FILE_PATH = Environment.getExternalStorageDirectory()+"/directorynameWhereYouWantTocreateDatabase";
    public static final String DATABASE_NAME = "mydb";
    public static final String TRACKS_TABLE = "tracks";
    public static final String TRACK_INFO_TABLE = "track_info";

    private static final String TRACKS_TABLE_CREATE = "create table " + TRACKS_TABLE
            + " (_id integer primary key autoincrement, title text not null, description text null, created_at date not null);";

    private static final String TRACK_INFO_TABLE_CREATE = "create table "
            + TRACK_INFO_TABLE
            + " (_id integer primary key autoincrement, track_id integer not null, latitude real not null, longitude real not null, altitude real not null, created_at date not null);";

    private SQLiteDatabase database;

    public DatabaseHelper() {
        try {
            database = SQLiteDatabase.openDatabase(DATABASE_FILE_PATH + File.separator + DATABASE_NAME, null, SQLiteDatabase.OPEN_READWRITE);
        } catch (SQLiteException ex) {
            Log.e(TAG, "error -- " + ex.getMessage(), ex);
            // error means tables does not exits
            createTables();
        } finally {
            close();
        }
    }

    private void createTables() {
        database.execSQL(TRACKS_TABLE_CREATE);
        database.execSQL(TRACK_INFO_TABLE_CREATE);
    }

    public synchronized void close() {
    if (database != null) {
        database.close();
    }

}

    public SQLiteDatabase getReadableDatabase() {
        database = SQLiteDatabase.openDatabase(DATABASE_FILE_PATH + File.separator + DATABASE_NAME, null, SQLiteDatabase.OPEN_READONLY);
        return database;
    }

    public SQLiteDatabase getWritableDatabase() {
        database = SQLiteDatabase.openDatabase(DATABASE_FILE_PATH + File.separator + DATABASE_NAME, null, SQLiteDatabase.OPEN_READWRITE);
        return database;
    }
}

And in the end you have to set permission in manifest like this: android.permission.WRITE_EXTERNAL_STORAGE

Good luck :)

Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77