How do I handle two different databases in android which in assets folder?or does android works on database having FILE format?I tries this code but getting no Parent_Category is found? After using Assets class i didn't succeed so i place my initial codes snippets.
DatabaseHandler File
public class DatabaseHandler extends SQLiteOpenHelper {
public static final String TAG = "DatabaseHandler";
private SQLiteDatabase db;
private Context activity;
String mydate;
SharedPreferences sh_Pref;
private String prefName = "MyPref";
String iname="no";
private String DB_PATH = "/data/data/com.pstalk" + "/databases/";
private static String DB_NAME = "state.sqlite";
public DatabaseHandler(Context context) throws IOException {
super(context, DB_NAME, null, 1);
this.activity = context;
boolean dbexist = checkdatabase();
if (dbexist) {
Log.d("Trong", "Database exists");
opendatabase();
} else {
System.out.println("Database doesn't exist");
createdatabase();
}
}
public DatabaseHandler(Activity mActivity, String idns)throws IOException {
// TODO Auto-generated constructor stub
super(mActivity, DB_NAME, null, 1);
this.iname = idns;
boolean dbexist = checkdatabase();
if (dbexist) {
Log.d("Trong", "Database exists");
opendatabase();
} else {
System.out.println("Database doesn't exist");
createdatabase();
}
}
public void createdatabase() throws IOException {
boolean dbexist = checkdatabase();
if (dbexist) {
// System.out.println(" Database exists.");
} else {
this.getReadableDatabase();
try {
copydatabase();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private boolean checkdatabase() {
// SQLiteDatabase checkdb = null;
boolean checkdb = false;
try {
String myPath = DB_PATH + DB_NAME;
Log.d("Trong", "DB_PATH + DB_NAME " + DB_PATH + DB_NAME);
File dbfile = new File(myPath);
// checkdb =
// SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READWRITE);
checkdb = dbfile.exists();
} catch (SQLiteException e) {
Log.d("Trong", "Database doesn't exist");
}
return checkdb;
}
private void copydatabase() throws IOException {
AssetManager am = activity.getAssets();
OutputStream os = new FileOutputStream(DB_PATH + DB_NAME);
byte[] b = new byte[1024];
String[] files = am.list("");
Arrays.sort(files);
int r;
InputStream is=null;
for (int i = 1; i <= 2; i++) {
if(i==1)
is = am.open("state.sqlite");
else
is=am.open("Category.sqlite");
while ((r = is.read(b)) != -1) {
os.write(b, 0, r);
}
Log.i("BABY_DATABASE_HELPER", "Copying the database (part " + i
+ " of 9)");
is.close();
}
os.close();
}
public void opendatabase() throws SQLException {
// Open the database
String mypath = DB_PATH + DB_NAME;
db = SQLiteDatabase.openDatabase(mypath, null,
SQLiteDatabase.OPEN_READWRITE);
}
public synchronized void close() {
if (db != null) {
db.close();
}
super.close();
}
public ArrayList<StateEntity> getAllState() {
// TODO Auto-generated method stub
ArrayList<StateEntity> flashcardentity = new ArrayList<StateEntity>();
String selectQuery;
selectQuery = "SELECT * FROM "+"STATETB ";
Cursor cursor = db.rawQuery(selectQuery, null);
Log.d("cursorfav", "c"+cursor.getCount()+" "+selectQuery);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Log.d("cursorfav", "cinside");
StateEntity fl = new StateEntity();
//itto.setId(cursor.getStringStateEntity0));
fl.setCid(cursor.getString(0));
fl.setSid(cursor.getString(1));
fl.setName(cursor.getString(2));
flashcardentity.add(fl);
} while (cursor.moveToNext());
}
return flashcardentity;
}
public ArrayList<CityEntity> getCity(String STATEID) {
// TODO Auto-generated method stub
ArrayList<CityEntity> flashcardentity = new ArrayList<CityEntity>();
String selectQuery;
selectQuery = "SELECT * FROM "+"CITYTB WHERE SID="+"'"+STATEID+"'";
Cursor cursor = db.rawQuery(selectQuery, null);
Log.d("cursorfav", "c"+cursor.getCount()+" "+selectQuery);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Log.d("cursorfav", "cinside");
CityEntity fl = new CityEntity();
fl.setCity_id(cursor.getString(0));
fl.setName(cursor.getString(1));
fl.setSid(cursor.getString(2));
flashcardentity.add(fl);
} while (cursor.moveToNext());
}
// return contact list
return flashcardentity;
}
public ArrayList<ProfessionEntity> getParentCategory(String User_typ){
ArrayList<ProfessionEntity> flashcardentity = new ArrayList<ProfessionEntity>();
String selectQuery;
selectQuery="SELECT * FROM "+"Upload_category WHERE User_typ="+"'"+User_typ+"'";
Cursor cursor=db.rawQuery(selectQuery, null);
Log.d("cursorfav", "c"+cursor.getCount()+""+selectQuery);
if(cursor.moveToFirst()){
do{
Log.d("cursorfav", "cinside");
ProfessionEntity pe=new ProfessionEntity();
pe.setUser_typ(cursor.getString(0));
pe.setParent_key(cursor.getInt(1));
pe.setParent(cursor.getString(2));
flashcardentity.add(pe);
}while(cursor.moveToNext());
}
return flashcardentity;
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
Aucun commentaire:
Enregistrer un commentaire