Skip to content

Instantly share code, notes, and snippets.

@fida1989
Created April 8, 2015 16:46
Show Gist options
  • Save fida1989/422f110ad7546a8b7f7e to your computer and use it in GitHub Desktop.
Save fida1989/422f110ad7546a8b7f7e to your computer and use it in GitHub Desktop.
package com.discoverbangladesh.utils;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import com.discoverbangladesh.models.Division;
import com.discoverbangladesh.models.Embassy;
import com.discoverbangladesh.models.Hotel;
import com.discoverbangladesh.models.Spot;
public class ExternalDbOpenHelper extends SQLiteOpenHelper {
// ���� � ����� � ������ �� ����������
public static String DB_PATH;
// ��� ����� � �����
public static String DB_NAME;
public SQLiteDatabase database;
public final Context context;
//
private static final String TABLE_DIV = "divisions";
private static final String DIV_ID = "id";
private static final String DIV_NAME = "name";
private static final String DIV_IMG = "image";
//
private static final String TABLE_SPOT = "spots";
private static final String SPOT_DIVID = "divid";
private static final String SPOT_ID = "id";
private static final String SPOT_NAME = "name";
private static final String SPOT_IMG = "image";
private static final String SPOT_DESC = "description";
private static final String SPOT_MAP = "map";
//
private static final String TABLE_EMB = "embassies";
private static final String EMB_ID = "id";
private static final String EMB_COUN = "country";
private static final String EMB_NAME = "name";
private static final String EMB_ADD = "address";
private static final String EMB_PHO = "phone";
private static final String EMB_FAX = "fax";
private static final String EMB_FLAG = "flag";
//
private static final String TABLE_HOT = "hotels";
private static final String HOT_ID = "id";
private static final String HOT_NAME = "name";
private static final String HOT_ADD = "address";
private static final String HOT_PHO = "phone";
private static final String HOT_EMA = "email";
private static final String HOT_DIVID = "divid";
public SQLiteDatabase getDb() {
return database;
}
public ExternalDbOpenHelper(Context context, String databaseName) {
super(context, databaseName, null, 1);
this.context = context;
// �������� ������ ���� � ����� ��� ������ ����������
String packageName = context.getPackageName();
DB_PATH = String.format("//data//data//%s//databases//", packageName);
DB_NAME = databaseName;
openDataBase();
}
// ������� ����, ���� ��� �� �������
public void createDataBase() {
boolean dbExist = checkDataBase();
if (!dbExist) {
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
Log.e(this.getClass().toString(), "Copying error");
throw new Error("Error copying database!");
}
} else {
Log.i(this.getClass().toString(), "Database already exists");
}
/*
* this.getReadableDatabase(); try { copyDataBase(); } catch
* (IOException e) { Log.e(this.getClass().toString(), "Copying error");
* throw new Error("Error copying database!"); }
*/
}
// �������� ������������� ���� ������
private boolean checkDataBase() {
SQLiteDatabase checkDb = null;
try {
String path = DB_PATH + DB_NAME;
checkDb = SQLiteDatabase.openDatabase(path, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLException e) {
Log.e(this.getClass().toString(), "Error while checking db");
}
// ������� �� ����� ������ ��������, ��� ������ �����������
if (checkDb != null) {
checkDb.close();
}
return checkDb != null;
}
// ����� ����������� ����
private void copyDataBase() throws IOException {
// ��������� ����� ��� ������ �� ��� ��������� ���� ��
// �������� � assets
InputStream externalDbStream = context.getAssets().open(DB_NAME);
// ���� � ��� ��������� ������ ���� � ��������
String outFileName = DB_PATH + DB_NAME;
// ������ �������� ����� ��� ������ � ��� �� ��������
OutputStream localDbStream = new FileOutputStream(outFileName);
// ���������� �����������
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = externalDbStream.read(buffer)) > 0) {
localDbStream.write(buffer, 0, bytesRead);
}
// �� ����� �������� ����������(���������) � ������� ������
localDbStream.close();
externalDbStream.close();
}
public SQLiteDatabase openDataBase() throws SQLException {
String path = DB_PATH + DB_NAME;
if (database == null) {
createDataBase();
database = SQLiteDatabase.openDatabase(path, null,
SQLiteDatabase.OPEN_READWRITE);
}
return database;
}
@Override
public synchronized void close() {
if (database != null) {
database.close();
}
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public ArrayList<Division> getDivs() {
ArrayList<Division> divisions = new ArrayList<Division>();
Cursor divCursor = database.query(TABLE_DIV, new String[] { DIV_ID,
DIV_NAME, DIV_IMG }, null, null, null, null, DIV_ID);
divCursor.moveToFirst();
if (!divCursor.isAfterLast()) {
do {
Division div = new Division(divCursor.getInt(0),
divCursor.getString(1), divCursor.getString(2)); // friendCursor.getString(1);
divisions.add(div);
} while (divCursor.moveToNext());
}
divCursor.close();
return divisions;
}
public ArrayList<Spot> getSpots(int divid) {
ArrayList<Spot> spots = new ArrayList<Spot>();
Cursor spotCursor = database.query(TABLE_SPOT, new String[] { SPOT_ID,
SPOT_NAME, SPOT_DESC, SPOT_IMG, SPOT_MAP }, SPOT_DIVID + "="
+ divid, null, null, null, SPOT_ID);
spotCursor.moveToFirst();
if (!spotCursor.isAfterLast()) {
do {
Spot spot = new Spot(spotCursor.getInt(0),
spotCursor.getString(1), spotCursor.getString(2),
spotCursor.getString(3), spotCursor.getString(4)); // friendCursor.getString(1);
spots.add(spot);
} while (spotCursor.moveToNext());
}
spotCursor.close();
// database.close();
return spots;
}
public ArrayList<Embassy> getEmbassies() {
ArrayList<Embassy> embassies = new ArrayList<Embassy>();
Cursor divCursor = database.query(TABLE_EMB, new String[] { EMB_ID,
EMB_COUN, EMB_NAME,EMB_ADD,EMB_PHO,EMB_FAX, EMB_FLAG}, null, null, null, null, EMB_ID);
divCursor.moveToFirst();
if (!divCursor.isAfterLast()) {
do {
Embassy emb = new Embassy(divCursor.getInt(0),
divCursor.getString(1), divCursor.getString(2),divCursor.getString(3),divCursor.getString(4),divCursor.getString(5),divCursor.getString(6)); // friendCursor.getString(1);
embassies.add(emb);
System.out.println(emb.country);
} while (divCursor.moveToNext());
}
divCursor.close();
System.out.println(embassies.size());
return embassies;
}
public ArrayList<Hotel> getHotels(int divid) {
ArrayList<Hotel> hotels = new ArrayList<Hotel>();
Cursor divCursor = database.query(TABLE_HOT, new String[] { HOT_ID,
HOT_NAME,HOT_ADD,HOT_PHO,HOT_EMA}, HOT_DIVID + "="
+ divid, null, null, null, HOT_ID);
divCursor.moveToFirst();
if (!divCursor.isAfterLast()) {
do {
Hotel hot = new Hotel(divCursor.getInt(0),
divCursor.getString(1), divCursor.getString(2),divCursor.getString(3),divCursor.getString(4)); // friendCursor.getString(1);
hotels.add(hot);
System.out.println(hot.name);
} while (divCursor.moveToNext());
}
divCursor.close();
System.out.println(hotels.size());
return hotels;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment