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>
android 同时打开两个sqlite database db的更多相关文章
- [转]Android Studio SQLite Database Multiple Tables Example
本文转自:http://instinctcoder.com/android-studio-sqlite-database-multiple-tables-example/ BY TAN WOON HO ...
- [转]Android | Simple SQLite Database Tutorial
本文转自:http://hmkcode.com/android-simple-sqlite-database-tutorial/ Android SQLite database is an integ ...
- [转]Android Studio SQLite Database Example
本文转自:http://instinctcoder.com/android-studio-sqlite-database-example/ BY TAN WOON HOW · PUBLISHED AP ...
- Android中多表的SQLite数据库(译)
原文: Android SQLite Database with Multiple Tables 在上一篇教程Android SQLite Database Tutorial中,解释了如何在你的And ...
- 在 Android 应用程序中使用 SQLite 数据库以及怎么用
part one : android SQLite 简单介绍 SQLite 介绍 SQLite 一个非常流行的嵌入式数据库.它支持 SQL 语言,而且仅仅利用非常少的内存就有非常好的性能.此外它还是开 ...
- Android学习---如何创建数据库,SQLite(onCreate,onUpgrade方法)和SQLiteStudio的使用
一.android中使用什么数据库? SQLite是遵守ACID的关系数据库管理系统,它包含在一个相对小的C程式庫中.它是D.RichardHipp建立的公有领域项目.SQLite 是一个软件库,实现 ...
- Android本地数据存储之SQLite关系型数据库 ——SQLiteDatabase
数据库的创建,获取,执行sql语句: 框架搭建:dao 思考: 1.数据库保存在哪里? 2.如何创建数据库?如何创建表? 3.如何更新数据库?如何更改表的列数据? 4.如何获取数据库? 5.如何修改数 ...
- android聊天,存储聊天记录sqlite
项目中有聊天模块,需要用到打开activity的时候初始化聊天记录的情况.大致情况如下: 辅助类:ChatSQLiteHelper 在第一次时会调用oncreate方法(判断的标准是schedul ...
- 在 Android Studio 上调试数据库 ( SQLite ) (转)
转自:http://c.colabug.com/thread-1781696-1-1.html 以前 Eclipse 时代,调试 SQLite 都是将数据库文件导出到电脑,然后再用软件打开查看.现在我 ...
随机推荐
- Session之Config配置
<sessionState mode="Off|InProc|StateServer|SQLServer" cookieless="true|false" ...
- 在Ubuntu18.04下将应用程序添加到启动器
# 在启动器里面给应用程序添加一个快捷方式 在linux(ubuntu)平台下,很多小伙伴发现,自己去官网下载解压的软件不能自动添加到启动器,每次启动的时候需要再次进入软件目录输入命令,非常不方便.本 ...
- windows上tomcat8的安装及配置
提示:在安装tomcat之前,确定安装好jdk. 一.下载tomcat8 http://tomcat.apache.org/download-80.cgi 点击这个网址 根据自己电脑的才做系统版本安装 ...
- 程序设计第三次作业---C++计算器雏形
Github链接:https://github.com/Wasdns/object-oriented 题目:程序设计第三次作业 程序设计第三次作业附加 我的程序设计第三次作业附加 代码规范 更新时间: ...
- 业务-----部门Service常用逻辑
1.org实体类 public class Org implements Serializable { private static final long serialVersionUID = 1L; ...
- 8 commands to check cpu information on Linux
https://www.binarytides.com/linux-cpu-information/
- FileUtils功能概述
https://commons.apache.org/proper/commons-io/javadocs/api-release/org/apache/commons/io/FileUtils.ht ...
- windows上面链接使用linux上面的docker daemon
1. 修改linux 上面的 docker的 配置文件. vim /usr/lib/systemd/system/docker.service 注意 这个是centos的路径 发现ubuntu的路径不 ...
- Bootstrap @Media分类
手机的屏幕比较小,宽度通常在600像素以下:PC的屏幕宽度,一般都在1000像素以上(目前主流宽度是1366×768)设置相应的min-width和max-width值 所以响应式设计一般对600 ...
- PSP(5.11——5.17)以及周记录
1.PSP 5.11 14:30 20:00 130 200 Cordova A Y min 5.12 9:00 14:00 100 200 Cordova A Y min 5.13 13:30 15 ...