DataBaseHelper.java 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package com.chinaappsremover.dbhandler;
  2. import android.content.Context;
  3. import android.database.Cursor;
  4. import android.database.sqlite.SQLiteDatabase;
  5. import android.database.sqlite.SQLiteOpenHelper;
  6. import android.util.Log;
  7. import com.chinaappsremover.wrapper.AppInfo;
  8. import java.io.File;
  9. import java.util.List;
  10. public class DataBaseHelper extends SQLiteOpenHelper {
  11. public static final String DATABASE_NAME = "rca.db";
  12. public static final int DATABASE_VERSION = 1;
  13. private Context myContext;
  14. private SQLiteDatabase sqLiteDatabase = getWritableDatabase();
  15. public DataBaseHelper(Context context) {
  16. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  17. myContext = context;
  18. }
  19. public void onCreate(SQLiteDatabase sQLiteDatabase) {
  20. Log.w("Db oncrate called", "Db oncreate called");
  21. sQLiteDatabase.execSQL("create table apps (id integer primary key autoincrement, p_name text, a_name text);");
  22. }
  23. public void onUpgrade(SQLiteDatabase sQLiteDatabase, int i, int i2) {
  24. myContext.getDatabasePath("dest.sqLiteDatabase");
  25. }
  26. public boolean attach(File file, boolean z) {
  27. try {
  28. sqLiteDatabase.execSQL("attach database ? as sqLiteDatabase", new String[]{file.getAbsolutePath()});
  29. if (!z) {
  30. sqLiteDatabase.delete("apps", null, null);
  31. }
  32. sqLiteDatabase.execSQL("INSERT INTO apps (p_name, a_name) SELECT p_name, a_name FROM sqLiteDatabase.apps");
  33. return file.delete();
  34. } catch (Exception e) {
  35. e.printStackTrace();
  36. return false;
  37. }
  38. }
  39. public List<AppInfo> isExist(List<AppInfo> list, List<AppInfo> list2) {
  40. list2.clear();
  41. for (AppInfo next : list) {
  42. Cursor rawQuery = sqLiteDatabase.rawQuery("select * from apps where p_name ='" + next.packageName + "'", null);
  43. if (rawQuery != null && rawQuery.moveToFirst()) {
  44. list2.add(next);
  45. while (rawQuery.moveToNext()) {
  46. list2.add(next);
  47. }
  48. }
  49. if (rawQuery != null) {
  50. rawQuery.close();
  51. }
  52. }
  53. return list2;
  54. }
  55. }