Featured post
sql - Android multiple databases open -
i making im client android , working databases storing contacts , other info's... in app have activity , 1 service. need open 3 databases in same time both on service , on activity.
i use 3 database because want databases managed more without having problems synchronization of writing in them. (as far know need write in database synchronously because may crush).
to manage databases service , activity in same time, thought singleton or static class of databasehelper me...
so have started make test making 2 databasehelper global objects in activity, each 1 opens diferent database, after running project i've noticed last opened database remains opened in both objects :((, why happening?
can sugest me how can make work? thank you!
l.e.: after more tests, made static object of databasehelper, open service take database object activity , in same time made 2 statements, 1 in activity , 1 in service run 0 3000 , adds values same database , reads database.
after run noticed database still in feet , running without errors. strange thing service running after activity finishes job. why that? thank you!
i have databaseadapter class contains 2 databases opened together.
public class databaseadapter { /** identifier internal database */ public static final int internal = 0; /** identifier external database */ public static final int external = 1; private final sqliteopenhelper[] mdatabasemanager = new sqliteopenhelper[2]; private final sqlitedatabase[] mdatabases = new sqlitedatabase[2]; /** * constructs database , open it. */ public databaseadapter() { // open internal_db mdatabasemanager[internal] = new internaldatabasemanager(myapplication.getinstance()); mdatabases[internal] = mdatabasemanager[internal].getwritabledatabase(); } /** * checks database state , throws {@link illegalstateexception} if database isn't open. * should used before starting access database. * * @param type type of database. can internal or external. */ public void checkdbstate(int type) { if (mdatabases[type] == null || !mdatabases[type].isopen()) { throw new illegalstateexception("the database has not been opened"); } } /** * closes database of given type. * * @param type type of database. can internal or external. */ public void close(int type) { if (mdatabases[type].isopen()) { mdatabases[type].close(); mdatabases[type] = null; if (mdatabasemanager[type] != null) { mdatabasemanager[type].close(); mdatabasemanager[type] = null; } } } /** * @param type type of database. can internal or external. * @return true if database open, false otherwise. */ public boolean isopen(int type) { return mdatabases[type] != null && mdatabases[type].isopen(); } /** * opens default database. * * @param type type of database. can internal or external. */ public void open(int type) { switch (type) { case internal: mdatabasemanager[internal] = new internaldatabasemanager(myapplication.getinstance()); if (!isopen(internal)) { mdatabases[internal] = mdatabasemanager[internal].getwritabledatabase(); } break; case external: mdatabasemanager[external] = new externaldatabasemanager(myapplication.getinstance(), constants.external_db_path, 1); if (!isopen(external)) { mdatabases[external] = mdatabasemanager[external].getwritabledatabase(); } break; } } }
to add third 1 should easy :)
- Get link
- X
- Other Apps
Comments
Post a Comment