Created
December 27, 2014 01:54
-
-
Save juwoong/1fe45c9303080936de52 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DbHelper { | |
private static final String DATABASE_NAME = "bus.db"; | |
private static final String PACKAGE_DIR = "/data/data/package_name/databases"; | |
public static SQLiteDatabase mDB; | |
private DataBaseHelper mDBHelper; | |
private Context ctx; | |
private class DataBaseHelper extends SQLiteOpenHelper { | |
public DataBaseHelper(Context context) { | |
super(context, PACKAGE_DIR + "/" + DATABASE_NAME, null, 1); | |
init(context); | |
} | |
public void init(Context context) { | |
Log.i("tag", "Start Init"); | |
File folder = new File(PACKAGE_DIR); | |
folder.mkdirs(); | |
File outfile = new File(PACKAGE_DIR + "/" + DATABASE_NAME); | |
if (outfile.length() <= 0) { | |
AssetManager assetManager = context.getResources().getAssets(); | |
try { | |
InputStream is = assetManager.open(DATABASE_NAME, AssetManager.ACCESS_BUFFER); | |
long filesize = is.available(); | |
byte[] tempdata = new byte[(int) filesize]; | |
is.read(tempdata); | |
is.close(); | |
outfile.createNewFile(); | |
FileOutputStream fo = new FileOutputStream(outfile); | |
fo.write(tempdata); | |
fo.close(); | |
Log.i("tag", "maybe success to copy file"); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
@Override | |
public void onCreate(SQLiteDatabase db) { | |
} | |
@Override | |
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | |
} | |
} | |
public DbHelper(Context context) { | |
this.ctx = context; | |
} | |
public DbHelper open() throws SQLException { | |
Log.i("tag", "start open"); | |
mDBHelper = new DataBaseHelper(ctx); | |
mDB = mDBHelper.getReadableDatabase(); | |
return this; | |
} | |
public void close() { | |
mDB.close(); | |
} | |
public void sendQuery(String sql, customBusStopAdapter adapter) throws SQLException { | |
Log.i("sendQuery",sql); | |
Cursor res = mDB.rawQuery(sql, null); | |
data data = new data(); | |
res.moveToFirst(); | |
while(!res.isAfterLast()) { | |
data.stName = res.getString(0); | |
data.stId = res.getString(1); | |
data.stLocation = res.getString(2); | |
adapter.m_list.add(data); | |
} | |
this.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment