private SQLiteDatabase db;
private DatabaseHelper dbHelper;
private Context context;

public DBAdapter(Context ctx) {
this.context = ctx;
dbHelper = new DatabaseHelper(context);
}

private class DatabaseHelper extends SQLiteOpenHelper {

public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, Version);
}

@Override
public void onCreate(SQLiteDatabase db) {
// create table
Log.i("onCreateTable",":");
db.execSQL(Category_CREATE);
db.execSQL(FAVORITE_CREATE);
db.execSQL(Message_CREATE);
db.execSQL(Key_Mess_CREATE);
db.execSQL(W_Message_LIST_CREATE);
db.execSQL(W_Message_CREATE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists "+Category_TABLE);
db.execSQL("drop table if exists "+FAVORITE_TABLE);
db.execSQL("drop table if exists "+Message_TABLE);
db.execSQL("drop table if exists "+Key_Mess_TABLE);
db.execSQL("drop table if exists "+W_Message_TABLE);
db.execSQL("drop table if exists "+W_Message_LIST);
onCreate(db);
}

}
public DBAdapter open() {
//db = context.openOrCreateDatabase(DATABASE_NAME, Context.MODE_PRIVATE, null);
try
{
// 获得dictionary.db文件的绝对路径
String databaseFilename = DATABASE_PATH + "/" + DATABASE_NAME;
File dir = new File(DATABASE_PATH);
// 如果/sdcard/dictionary目录中存在,创建这个目录
if (!dir.exists())
dir.mkdir();
// 如果在/sdcard/dictionary目录中不存在
// dictionary.db文件,则从res/raw目录中复制这个文件到
// SD卡的目录(/sdcard/dictionary)
File file = new File( DATABASE_PATH + "/" + DATABASE_NAME_OLD);
if(file.exists()){
Log.i(databaseFilename,"delete db");
file.delete();
}
if (!(new File(databaseFilename)).exists())
{
Log.i(databaseFilename,"copy db");
// 获得封装dictionary.db文件的InputStream对象
InputStream is = context.getResources().openRawResource(
R.raw.wpp);
FileOutputStream fos = new FileOutputStream(databaseFilename);
byte[] buffer = new byte[8192];
int count = 0;
// 开始复制dictionary.db文件
while ((count = is.read(buffer)) > 0)
{
fos.write(buffer, 0, count);
}

fos.close();
is.close();
}
// 打开/sdcard/dictionary目录中的dictionary.db文件
db = SQLiteDatabase.openOrCreateDatabase(
databaseFilename, null);
return this;
}
catch (Exception e)
{
}
return null;
}

public void close() {
try {
dbHelper.close();
} catch (Exception e) {

}

}

更多相关文章

  1. Android(安卓)SQLite数据库的添 删 查 改
  2. Android文件图片上传的详细讲解(三)---模式回调类
  3. android GPS JAVA应用程序编程-------获得经纬度,卫星信息等
  4. Android(安卓)Bluetooth 文件接收路径修改方法
  5. 如何更换Android系统默认字体(Android6.0)
  6. android webview file标签点击弹出选择文件或拍照菜单
  7. Android(安卓)VideoView简单播放视频
  8. 用Android(安卓)NDK编译FFmpeg
  9. NPM 和webpack 的基础使用

随机推荐

  1. Android的App Widget实现
  2. 总结Android(安卓)Socket开发中可能遇到
  3. android 遮罩层效果
  4. Android(安卓)9.1 定制开机向导
  5. 修改Android中strings.xml文件, 动态改变
  6. 学习Android(安卓)SDK Samples之旅-conne
  7. 789高手出招,国产手机的崛起路
  8. Android内存管理哪些事
  9. Android(安卓)Studio官方文档之使用Lint
  10. Drawable的Tint变色(让Android也能有iOS那