Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
upgrade a database without removing the data - code segment one
public class DatabaseHelper extends SQLiteOpenHelper {
//The Android's default system path of your application database.
private static String DB_PATH = "/data/data/test.test/databases/";
private static String DB_NAME = "TestDatabase";
private static final int DB_VERSION = 1;
private SQLiteDatabase myDatabase;
private final Context myContext;
* # Constructor #
* Takes and keeps a reference of the passed context in order to access to the application assets and resources.
* @param context
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.myContext = context;
* # Create Database #
* Creates a empty database on the system and rewrites it with your own database.
public void createDatabase() throws IOException {
boolean dbExist = checkDatabase();
if (dbExist) {
//do nothing - database already exist
else {
//By calling this method and empty database will be created into the default system path
//of your application so we are gonna be able to overwrite that database with our database.
try {
} catch (IOException e) {
throw new Error("Error copying database");
private boolean checkDatabase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
//database does't exist yet.
if (checkDB != null) {
return checkDB != null ? true : false;
private void copyDatabase() throws IOException {
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = > 0) {
myOutput.write(buffer, 0, length);
//Close the streams
// # open database #
public void openDatabase() throws SQLException {
//Open the database
String myPath = DB_PATH + DB_NAME;
myDatabase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
public synchronized void close() {
if (myDatabase != null)
public void onCreate(SQLiteDatabase db) {
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
public List<String> selectData
(String tableName, String[] columns, String selection, String[] selectionArgs,
String groupBy, String having, String orderBy) {
List<String> list = new ArrayList<String>();
Cursor cursor = this.myDatabase.query(tableName, columns, selection, selectionArgs, groupBy, having, orderBy);
if (cursor.moveToFirst()) {
do {
while (cursor.moveToNext());
if (cursor != null && !cursor.isClosed()) {
return list;
public void insertData(String tableName, String nullColumnHack, ContentValues values) {
try {
myDatabase.insert(tableName, nullColumnHack, values);
} catch (Exception e) {
Log.e("Error :", "unable to insert data");
//edit row
public void updateData(String tableName, ContentValues values, String whereClause, String[] whereArgs) {
try {
myDatabase.update(tableName, values, whereClause, whereArgs);
} catch (Exception e) {
Log.e("Error :", "unable to update data");
public void deleteRow(String tableName, String whereClause, String[] whereArgs) {
try {
myDatabase.delete(tableName, whereClause, whereArgs);
} catch (Exception e) {
Log.e("Error :", "unable to delete row");

This comment has been minimized.

Copy link
Owner Author

@Logan676 Logan676 commented Jun 11, 2015

this code segment doesnt work, you dont want to use this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment