同时打开两个db。

private void populateTabs(int TabShown) {    // Get specific setup info from database into cursor    setupDBHelper = new DBAdapter(this);    setupDBHelper.open(DBAdapter.MAIN_DATABASE_NAME, DBAdapter.MAIN_DATABASE_VERSION);

    setupCursor = setupDBHelper.fetchSetup(RowId);    this.startManagingCursor(setupCursor);    setupCursor.moveToFirst();

    offsetDBHelper = new DBAdapter(this);    offsetDBHelper.open(DBAdapter.OFFSET_DATABASE_NAME, DBAdapter.OFFSET_DATABASE_VERSION);

    offsetCursor = offsetDBHelper.fetchOffset(setupCursor.getString(setupCursor            .getColumnIndex(DBAdapter.SETUP_PART)));    this.startManagingCursor(offsetCursor);    offsetCursor.moveToFirst();}

private void populateTabs(int TabShown) {
    // Get specific setup info from database into cursor
    setupDBHelper = new DBAdapter(this);
    setupDBHelper.open(DBAdapter.MAIN_DATABASE_NAME, DBAdapter.MAIN_DATABASE_VERSION);

    offsetDBHelper = new DBAdapter(this);
    offsetDBHelper.open(DBAdapter.OFFSET_DATABASE_NAME, DBAdapter.OFFSET_DATABASE_VERSION);

    setupCursor = setupDBHelper.fetchSetup(RowId);
    this.startManagingCursor(setupCursor);
    setupCursor.moveToFirst();      

    offsetCursor = offsetDBHelper.fetchOffset(setupCursor.getString(setupCursor
            .getColumnIndex(DBAdapter.SETUP_PART)));
    this.startManagingCursor(offsetCursor);
    offsetCursor.moveToFirst();

}

public class DBAdapter {
// DB info
public static final String MAIN_DATABASE_NAME = "Bowers";
public static final String OFFSET_DATABASE_NAME = "BowersOffset";
public static final String NOTES_DATABASE_NAME = "Notes";
public static final String GMCODES_DATABASE_NAME = "GMCodes";
public static final int MAIN_DATABASE_VERSION = 1;
public static final int OFFSET_DATABASE_VERSION = 1;
public static final int NOTES_DATABASE_VERSION = 1;
public static final int GMCODES_DATABASE_VERSION = 1;

public static String MAIN_DB_PATH = "/data/data/cdc.workshopapps.BowersMfgHelper/databases/";

// database control
private DatabaseHelper mDbHelper;
private static SQLiteDatabase mDb;
private static Context mCtx;

private static class DatabaseHelper extends SQLiteOpenHelper {
    DatabaseHelper(Context context, String dbname, int dbversion) {
        super(context, dbname, null, dbversion);
        if (checkDataBase(dbname)) {
            openDataBase(dbname);
        } else {
            try {
                this.getReadableDatabase();
                copyDataBase(dbname);
                this.close();
                openDataBase(dbname);
            } catch (IOException e) {
                throw new Error("Error copying database");
            }
            Toast.makeText(context,
                    "Initial " + dbname + " database has been created",
                    Toast.LENGTH_LONG).show();
        }
    }

@Override
    public void onCreate(SQLiteDatabase db) {
    }

@Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }
}

public DBAdapter(Context ctx) {
    DBAdapter.mCtx = ctx;
}

public DBAdapter open(String dbname, int dbversion) throws SQLException {
    mDbHelper = new DatabaseHelper(mCtx, dbname, dbversion);
    mDb = mDbHelper.getWritableDatabase();
    return this;
}

public void close() {
    mDbHelper.close();
}

private static void copyDataBase(String dbname) throws IOException {
    InputStream myInput = mCtx.getAssets().open(dbname);
    String outFileName = MAIN_DB_PATH + dbname;
    OutputStream myOutput = new FileOutputStream(outFileName);

byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
    }

myOutput.flush();
    myOutput.close();
    myInput.close();
}

private static boolean checkDataBase(String dbname) {
    SQLiteDatabase checkDB = null;
    boolean exist = false;
    try {
        String db = MAIN_DB_PATH + dbname;
        checkDB = SQLiteDatabase.openDatabase(db, null,
                SQLiteDatabase.OPEN_READONLY);
    } catch (SQLiteException e) {
        Log.v("db log", "database does‘t exist");
    }

if (checkDB != null) {
        exist = true;
        checkDB.close();
    }
    return exist;
}

public static void openDataBase(String dbname) throws SQLException {
    String dbPath = MAIN_DB_PATH + dbname;
    mDb = SQLiteDatabase.openDatabase(dbPath, null,
            SQLiteDatabase.OPEN_READWRITE);
}

//  public Cursor fetchOffset(String part) throws SQLException {
//      Cursor mCursor = mDb.query(true, OFFSET_TABLE, new String[] {
//              OFFSET_ROWID, OFFSET_PART, SETUP_D1, SETUP_D2, SETUP_D3,
//              SETUP_D4, SETUP_D5, SETUP_D6, SETUP_D7, SETUP_D8, SETUP_D9,
//              SETUP_D10, SETUP_D11, SETUP_D12, SETUP_D13, SETUP_D14,
//              SETUP_D15, SETUP_D16, SETUP_D17, SETUP_D18, SETUP_D19,
//              SETUP_D20, SETUP_G54, SETUP_G55, SETUP_G56, SETUP_G57,
//              SETUP_G58, SETUP_G59, SETUP_G110, SETUP_G111, SETUP_G112,
//              SETUP_G113, SETUP_G114, SETUP_G115, SETUP_G116, SETUP_G117,
//              SETUP_G118, SETUP_G119, SETUP_G120, SETUP_G121, SETUP_G122,
//              SETUP_G123 }, OFFSET_PART + "= ‘" + part + "‘", null, null,
//              null, null, null);
//      if (mCursor != null) {
//          mCursor.moveToFirst();
//      }
//      return mCursor;
//  }

public Cursor fetchSetup(Long rowId) throws SQLException {
    Cursor mCursor = mDb.query(true, SETUP_TABLE, new String[] {
            SETUP_ROWID, SETUP_PART, SETUP_PGM1, SETUP_PGM2, SETUP_PGM3,
            SETUP_T1, SETUP_T2, SETUP_T3, SETUP_T4, SETUP_T5, SETUP_T6,
            SETUP_T7, SETUP_T8, SETUP_T9, SETUP_T10, SETUP_T11, SETUP_T12,
            SETUP_T13, SETUP_T14, SETUP_T15, SETUP_T16, SETUP_T17,
            SETUP_T18, SETUP_T19, SETUP_T20, SETUP_KIT, SETUP_FIXTURE1,
            SETUP_FIXTURE2, SETUP_FIXTURELOC, SETUP_VISE, SETUP_JAWS,
            SETUP_JAWLOC, SETUP_NOTES, SETUP_MISC1, SETUP_MISC2,
            SETUP_MISC3, SETUP_D1, SETUP_D2, SETUP_D3, SETUP_D4, SETUP_D5,
            SETUP_D6, SETUP_D7, SETUP_D8, SETUP_D9, SETUP_D10, SETUP_D11,
            SETUP_D12, SETUP_D13, SETUP_D14, SETUP_D15, SETUP_D16,
            SETUP_D17, SETUP_D18, SETUP_D19, SETUP_D20, SETUP_G54,
            SETUP_G55, SETUP_G56, SETUP_G57, SETUP_G58, SETUP_G59,
            SETUP_G110, SETUP_G111, SETUP_G112, SETUP_G113, SETUP_G114,
            SETUP_G115, SETUP_G116, SETUP_G117, SETUP_G118, SETUP_G119,
            SETUP_G120, SETUP_G121, SETUP_G122, SETUP_G123 }, TOOL_ROWID
            + "=" + rowId, null, null, null, null, null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}
}

---------------------------------------------------------

 

I have a DatabaseAdapter class which contains two databases which are opened together.

public class DatabaseAdapter {
    /** Identifier for the internal database */
    public static final int             INTERNAL            = 0;
    /** Identifier for the external database */
    public static final int             EXTERNAL                = 1;

    private final SQLiteOpenHelper[]    mDatabaseManager    = new SQLiteOpenHelper[2];
    private final SQLiteDatabase[]      mDatabases          = new SQLiteDatabase[2];

    /**
     * Constructs the database and open it.
     */
    public DatabaseAdapter() {
        // Open the internal_db
        mDatabaseManager[INTERNAL] = new InternalDatabaseManager(MyApplication.getInstance());
        mDatabases[INTERNAL] = mDatabaseManager[INTERNAL].getWritableDatabase();
    }

    /**
     * Checks the database state and throws an {@link IllegalStateException} if database isn‘t open.
     * Should always be used before starting to access the database.
     *
     * @param type Type of the database. Can be 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 the database of the given type.
     *
     * @param type Type of the database. Can be 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 the database. Can be INTERNAL or EXTERNAL.
     * @return true if the database is open, false otherwise.
     */
    public boolean isOpen(int type) {
        return mDatabases[type] != null && mDatabases[type].isOpen();
    }

    /**
     * Opens the default database.
     *
     * @param type Type of the database. Can be 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;
        }
    }
}

时间: 2024-10-01 04:45:20

同时打开两个db。的相关文章

thinkphp + easyUI 不能同时打开两张数据表?

两个linkbutton分别打开一个数据库中不同的两张数据表 现在单独打开两个是可以正确显示的. 但是如果打开了一个linkbutton1,不关掉tabs中对应的linkbutton1,再打开linkbutton2,这时linkbutton2 显示不了数据,但是如果关掉linkbutton1,再打开linkbutton2,是可以正确显示的. 求解 --------解决方案-------- 这是不可能的!thinkphp 每个表是一个对象,不可能出现你说的现象

同一台机子 打开两个tomcat

因为项目测试需要,需要在机子上单独搭建一个图片服务器,所以就要同时打开两个tomcat,这里记录方法. 1.首先复制一个新的tomcat程序: 2.在新的tomcat程序中修改server.xml的三个端口号: 一.<Server port="8005" shutdown="SHUTDOWN">,修改为8050 二. <Connector port="8080" protocol="HTTP/1.1",修为为

【Android开发那点破事】消息推送BroadcastReceiver,点击通知打开两次Activity问题

Android开发中,通常会使用BroadcastReceiver来接受Push推送消息.当APP收到推送通知时,我们需要在通知的点击事件中加入自己的逻辑.比如跳转到MainActivity.比如下面的代码(注意红色部分): public void onReceive(Context context, Intent intent) { Bundle bundle = intent.getExtras(); if (JPushInterface.ACTION_REGISTRATION_ID.equ

一个链接打开两个地址

<html><head><title>一个链接打开两个地址</title><script LANGUAGE="JavaScript">function hrefClick(newWin, locationWin) {   window.open(newWin);          //打开新的窗口   window.location = locationWin;//当前位置窗口也改变}</script></h

MySQL数据库远程访问权限如何打开(两种方法)

MySQL数据库远程访问权限如何打开(两种方法) 下载GPL版本安装 MySQL Community Edition(GPL) Community (GPL) Downloads 在我们使用mysql数据库时,有时我们的程序与数据库不在同一机器上,这时我们需要远程访问数据库.缺省状态下,mysql的用户没有远程访问的权限. 下面介绍两种方法,解决这一问题. 1.改表法 可能是你的帐号不允许从远程登陆,只能在localhost.这个时候只要在localhost的那台电脑,登入mysql后,更改 "

初学视觉学习笔记----保存并显示图片(打开两个相机)

以下代码可以实现的功能: 打开两个相机:抓拍图片以及获取图片的其他信息(如创建的时间):图像处理并把结果显示在hWindowCountrl:把图像在PicBox上显示(图片适应窗口大小): 查看原图(窗口适应图片的大小)//我觉得这个方法不太好用,再学习学习,找机会修改一下. 用到的相机: USB相机,S500A3B高拍仪(或者笔记本自带相机) 楼主还是一个小白,程序可能写的不好,但是均在自己电脑上运行成功^^,有需要的可以按需自取. using System;using System.Coll

同一链接点击后打开两个不同的地址

点击一个文字链接,分别打开两个窗口,每个窗口的网址不一样,打开的方式也不一样,一个是在自身窗口,一个是新开窗口,原来链接也可以这样用,是不是让大家也很吃惊呢? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http:/

点击链接打开两个页面

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>点击一个链接同时打开两个页面</title> <script type="te

怎么在CAD看图中同时打开两张CAD图纸进行查看?

怎么在CAD看图中同时打开两张CAD图纸进行查看?但我们在查看CAD图纸的时候,CAD图纸在编辑的过程中都是借助CAD编辑器来进行绘制的,格式都是dwg格式的,所以寻妖借助CAD看图软件来进行查看,那怎么在CAD看图中同时打开两张CAD图纸进行查看呢?小伙伴们都知道吗?具体要怎么来进行操作?下面我们就一起来看看具体操作步骤吧,希望能够帮助到你们. 第一步:首先在电脑桌面中的浏览器中搜索迅捷CAD看图,在搜索出的结果中点击下载安装最新版本的CAD看图软件进行使用,如果你的电脑中有这一款软件的就不用