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>
 

android 同时打开两个sqlite database db的更多相关文章

  1. [转]Android Studio SQLite Database Multiple Tables Example

    本文转自:http://instinctcoder.com/android-studio-sqlite-database-multiple-tables-example/ BY TAN WOON HO ...

  2. [转]Android | Simple SQLite Database Tutorial

    本文转自:http://hmkcode.com/android-simple-sqlite-database-tutorial/ Android SQLite database is an integ ...

  3. [转]Android Studio SQLite Database Example

    本文转自:http://instinctcoder.com/android-studio-sqlite-database-example/ BY TAN WOON HOW · PUBLISHED AP ...

  4. Android中多表的SQLite数据库(译)

    原文: Android SQLite Database with Multiple Tables 在上一篇教程Android SQLite Database Tutorial中,解释了如何在你的And ...

  5. 在 Android 应用程序中使用 SQLite 数据库以及怎么用

    part one : android SQLite 简单介绍 SQLite 介绍 SQLite 一个非常流行的嵌入式数据库.它支持 SQL 语言,而且仅仅利用非常少的内存就有非常好的性能.此外它还是开 ...

  6. Android学习---如何创建数据库,SQLite(onCreate,onUpgrade方法)和SQLiteStudio的使用

    一.android中使用什么数据库? SQLite是遵守ACID的关系数据库管理系统,它包含在一个相对小的C程式庫中.它是D.RichardHipp建立的公有领域项目.SQLite 是一个软件库,实现 ...

  7. Android本地数据存储之SQLite关系型数据库 ——SQLiteDatabase

    数据库的创建,获取,执行sql语句: 框架搭建:dao 思考: 1.数据库保存在哪里? 2.如何创建数据库?如何创建表? 3.如何更新数据库?如何更改表的列数据? 4.如何获取数据库? 5.如何修改数 ...

  8. android聊天,存储聊天记录sqlite

    项目中有聊天模块,需要用到打开activity的时候初始化聊天记录的情况.大致情况如下: 辅助类:ChatSQLiteHelper   在第一次时会调用oncreate方法(判断的标准是schedul ...

  9. 在 Android Studio 上调试数据库 ( SQLite ) (转)

    转自:http://c.colabug.com/thread-1781696-1-1.html 以前 Eclipse 时代,调试 SQLite 都是将数据库文件导出到电脑,然后再用软件打开查看.现在我 ...

随机推荐

  1. C#_面试

    class Program { static void Main(string[] args) { , , , , }; var arry = ConvertSum(arr); , , , , , } ...

  2. Jvm远程监控

    服务器运行新建文件 : udi.policy grant codebase "file:${java.home}/../lib/tools.jar" { permission ja ...

  3. Html5前端笔记

    获取Dpi 在 window.load中添加: (function(){ if (!window.screen.deviceXDPI){ var tmpNode = document.createEl ...

  4. Centos下部署DRBD+NFS+Keepalived高可用环境记录

    使用NFS服务器(比如图片业务),一台为主,一台为备.通常主到备的数据同步是通过rsync来做(可以结合inotify做实时同步).由于NFS服务是存在单点的,出于对业务在线率和数据安全的保障,可以采 ...

  5. C. The Tower is Going Home

    链接 [http://codeforces.com/contest/1075/problem/C] 题意 有个1e9*1e9的棋盘(1,1)位置在左下角也就是车这枚棋子的位置,然后有n个在某一列后面划 ...

  6. 快速排序 O(nlogn)

    #include<bits/stdc++.h> using namespace std; int a[200],n; void q_sort(int l,int r){ if(l>r ...

  7. java计算器项目

    简单的java计算器项目   题目:java计算器项目 一. 题目简介: 一个能进行加减乘除四则运算的小程序 Github链接:https://github.com/lizhenbin/test/tr ...

  8. Knowledge-Defined Networking

    知识定义的网络(Knowledge-Defined Networking) 来源:ACM SIGCOMM Computer Communication Review 年份:2017 是什么:容纳和利用 ...

  9. 加载spring容器

    import org.springframework.context.ApplicationContext; import org.springframework.context.support.Cl ...

  10. 研究VCL源码的原因和起点

    ---恢复内容开始--- 研究VCL源码的原因和起点 根本原因:当然是希望自己成为Delphi高手,因为这么多年过去,觉得自己始终不得要领,修改一个控件都无从下手,一直都只是个会拖控件的白痴.而我却拥 ...