@mSolo
2015-04-21T21:47:14.000000Z
字数 2657
阅读 1774
Android
ContentProvider
数据库
|---ContentProviderTranRec.java
|---SchemaHelperTranDailyRec.java
|---SchemaTranRec.java
|------SchemaTranDailyRec.java
|------SchemaTranMQYRec.java
public class ContentProviderTranRec extends ContentProvider {
// ...
private static final int TRAN_DAILY_REC_DIR = 1;
private static final int TRAN_DAILY_REC_ITEM = 2;
private static final int TRAN_DAILY_REC_ITEM_LIMIT = 3;
private static final int TRAN_DAILY_REC_ITEM_RANGE = 4;
private static final int TRAN_MQY_REC_DIR = 11;
private static final int TRAN_MQY_REC_ITEM = 12;
private static final int TRAN_MQY_REC_ITEM_LIMIT = 13;
private static final int TRAN_MQY_REC_ITEM_RANGE = 14;
static {
tranRecUriMatcher.addURI(AUTHORITY, "records/daily", TRAN_DAILY_REC_DIR);
tranRecUriMatcher.addURI(AUTHORITY, "records/daily/#", TRAN_DAILY_REC_ITEM);
tranRecUriMatcher.addURI(AUTHORITY, "records/daily/limit/#", TRAN_DAILY_REC_ITEM_LIMIT);
tranRecUriMatcher.addURI(AUTHORITY, "records/daily/range/#", TRAN_DAILY_REC_ITEM_RANGE);
tranRecUriMatcher.addURI(AUTHORITY, "records/mqy", TRAN_MQY_REC_DIR);
tranRecUriMatcher.addURI(AUTHORITY, "records/mqy/#", TRAN_MQY_REC_ITEM);
tranRecUriMatcher.addURI(AUTHORITY, "records/mqy/limit/#", TRAN_MQY_REC_ITEM_LIMIT);
tranRecUriMatcher.addURI(AUTHORITY, "records/mqy/range/#", TRAN_MQY_REC_ITEM_RANGE);
}
private SQLiteDatabase dailyDb = null;
private SQLiteDatabase mqyDb = null;
@Override
public boolean onCreate() {
dailyDb = new SchemaHelperTranDailyRec().getWritableDatabase();
mqyDb = new SchemaHelperTranMQYRec().getWritableDatabase();
return true;
}
}
public class SchemaHelperTranDailyRec extends SQLiteOpenHelper {
private String tableName = null;
private SchemaTranDailyRec schemaTranDailyRec = null;
private String templateTableName = null;
protected SchemaHelperTranDailyRec() {
super(StockeyeApp.appContext, SchemaTranDailyRec.DATABASE_NAME, null, SchemaTranDailyRec.DATABASE_VERSION);
schemaTranDailyRec = new SchemaTranDailyRec();
templateTableName = schemaTranDailyRec.getDatabaseTemplateTableName();
}
@Override
public SQLiteDatabase getWritableDatabase() {
File dbFile = new File( getDatabaseFilePath() + "/" + schemaTranDailyRec.getDatabaseName() );
if (!dbFile.exists()) {
try {
dbFile.createNewFile();
} catch (IOException ioe) {
return null;
}
}
return SQLiteDatabase.openOrCreateDatabase(dbFile, null);
}
private String getDatabaseFilePath() {
String dbPath = Environment.getExternalStorageDirectory().getParentFile().getAbsolutePath() + "/sdcard1/stockeye/database";
File dbDir = new File(dbPath);
if (!dbDir.exists() && !dbDir.mkdirs()) {
dbPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/stockeye/database";
dbDir = new File(dbPath);
if (!dbDir.exists() && !dbDir.mkdirs()) {
return null;
}
}
return dbPath;
}
}