[关闭]
@mSolo 2015-04-21T13:47:14.000000Z 字数 2657 阅读 1729

Android 多数据库设计实战(ContentProvider)

Android ContentProvider 数据库


更多代码请参考我的个人作品:Stockeye

1. 数据库项目结构

  1. |---ContentProviderTranRec.java
  2. |---SchemaHelperTranDailyRec.java
  3. |---SchemaTranRec.java
  4. |------SchemaTranDailyRec.java
  5. |------SchemaTranMQYRec.java

2. 创建多个数据库

  1. public class ContentProviderTranRec extends ContentProvider {
  2. // ...
  3. private static final int TRAN_DAILY_REC_DIR = 1;
  4. private static final int TRAN_DAILY_REC_ITEM = 2;
  5. private static final int TRAN_DAILY_REC_ITEM_LIMIT = 3;
  6. private static final int TRAN_DAILY_REC_ITEM_RANGE = 4;
  7. private static final int TRAN_MQY_REC_DIR = 11;
  8. private static final int TRAN_MQY_REC_ITEM = 12;
  9. private static final int TRAN_MQY_REC_ITEM_LIMIT = 13;
  10. private static final int TRAN_MQY_REC_ITEM_RANGE = 14;
  11. static {
  12. tranRecUriMatcher.addURI(AUTHORITY, "records/daily", TRAN_DAILY_REC_DIR);
  13. tranRecUriMatcher.addURI(AUTHORITY, "records/daily/#", TRAN_DAILY_REC_ITEM);
  14. tranRecUriMatcher.addURI(AUTHORITY, "records/daily/limit/#", TRAN_DAILY_REC_ITEM_LIMIT);
  15. tranRecUriMatcher.addURI(AUTHORITY, "records/daily/range/#", TRAN_DAILY_REC_ITEM_RANGE);
  16. tranRecUriMatcher.addURI(AUTHORITY, "records/mqy", TRAN_MQY_REC_DIR);
  17. tranRecUriMatcher.addURI(AUTHORITY, "records/mqy/#", TRAN_MQY_REC_ITEM);
  18. tranRecUriMatcher.addURI(AUTHORITY, "records/mqy/limit/#", TRAN_MQY_REC_ITEM_LIMIT);
  19. tranRecUriMatcher.addURI(AUTHORITY, "records/mqy/range/#", TRAN_MQY_REC_ITEM_RANGE);
  20. }
  21. private SQLiteDatabase dailyDb = null;
  22. private SQLiteDatabase mqyDb = null;
  23. @Override
  24. public boolean onCreate() {
  25. dailyDb = new SchemaHelperTranDailyRec().getWritableDatabase();
  26. mqyDb = new SchemaHelperTranMQYRec().getWritableDatabase();
  27. return true;
  28. }
  29. }
  1. public class SchemaHelperTranDailyRec extends SQLiteOpenHelper {
  2. private String tableName = null;
  3. private SchemaTranDailyRec schemaTranDailyRec = null;
  4. private String templateTableName = null;
  5. protected SchemaHelperTranDailyRec() {
  6. super(StockeyeApp.appContext, SchemaTranDailyRec.DATABASE_NAME, null, SchemaTranDailyRec.DATABASE_VERSION);
  7. schemaTranDailyRec = new SchemaTranDailyRec();
  8. templateTableName = schemaTranDailyRec.getDatabaseTemplateTableName();
  9. }
  10. @Override
  11. public SQLiteDatabase getWritableDatabase() {
  12. File dbFile = new File( getDatabaseFilePath() + "/" + schemaTranDailyRec.getDatabaseName() );
  13. if (!dbFile.exists()) {
  14. try {
  15. dbFile.createNewFile();
  16. } catch (IOException ioe) {
  17. return null;
  18. }
  19. }
  20. return SQLiteDatabase.openOrCreateDatabase(dbFile, null);
  21. }
  22. private String getDatabaseFilePath() {
  23. String dbPath = Environment.getExternalStorageDirectory().getParentFile().getAbsolutePath() + "/sdcard1/stockeye/database";
  24. File dbDir = new File(dbPath);
  25. if (!dbDir.exists() && !dbDir.mkdirs()) {
  26. dbPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/stockeye/database";
  27. dbDir = new File(dbPath);
  28. if (!dbDir.exists() && !dbDir.mkdirs()) {
  29. return null;
  30. }
  31. }
  32. return dbPath;
  33. }
  34. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注