android 同时打开两个sqlite database db

1,数据库类

package com.example.testdb;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBAdapter {

    // DB info
    public static String MAIN_DATABASE_NAME = "Bowers.db";
    public static final String OFFSET_DATABASE_NAME = "BowersOffset.db";
    public static final int MAIN_DATABASE_VERSION = 1;
    public static final int OFFSET_DATABASE_VERSION = 1;

    // database control
    private DatabaseHelper mDbHelper;
    private  SQLiteDatabase mDb;
    private  Context mCtx;
    public String currentDBName;
    public int currentDBVer;

    private  class DatabaseHelper extends SQLiteOpenHelper {
        DatabaseHelper(Context context, String dbname, int dbversion) {
            super(context, dbname, null, dbversion);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            Log.e("xxxx","onCreate");
            db.execSQL("CREATE TABLE `room` (`roomName` VARCHAR , `clazzId` INTEGER DEFAULT 0 , `createTime` BIGINT , `roomId` BIGINT , `check_code` BIGINT , `roomType` INTEGER , PRIMARY KEY (`roomId`) );");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.e("xxxx","onCreate");            
        }
    }

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

    public DBAdapter open(String dbname, int dbversion) throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx, dbname, dbversion);
        mDb = mDbHelper.getWritableDatabase();
        
        currentDBName = dbname;
        currentDBVer=dbversion;
        
        return this;
    }
    
    public void insert() {
        mDb.execSQL("Replace into room(roomName, roomType ) values( '"+currentDBName+"',"+currentDBVer+");");
    }


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

    //指定数据库文件路径方法打开数据库。
    static String MAIN_DB_PATH="/data/data/com.example.testdb/databases/";
    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  void openDataBase(String dbname) throws SQLException {
        String dbPath = MAIN_DB_PATH + dbname;
        mDb = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE);
    }

}

2,activity

package com.example.testdb;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        DBAdapter setupDBHelper = new DBAdapter(this);
        setupDBHelper.open(DBAdapter.MAIN_DATABASE_NAME, DBAdapter.MAIN_DATABASE_VERSION);
        setupDBHelper.insert();

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

    
    
    //The following is useless.
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

 

3,manifest xml

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testdb"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.testdb.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 

 
posted @ 2015-04-09 22:46  Bigben  阅读(827)  评论(0编辑  收藏  举报