SQLiteOpenHelper -Datenbankansatz mit manuellem Kopieren für Android -Bereitstellungen?Java

Java-Forum
Anonymous
 SQLiteOpenHelper -Datenbankansatz mit manuellem Kopieren für Android -Bereitstellungen?

Post by Anonymous »

Ich habe eine Android -Java -App, die eine interne SQLite -Datenbank mit einer Datenbank verwendet, die sich im Asset -Ordner befindet. Also kopiere ich die Datenbank im Konstruktor "manuell". Meine Frage ist, wenn dies ein gültiger Ansatz für die Android-Bereitstellung ist, unter Berücksichtigung der Möglichkeit von Aktualisierungen oder möglicherweise zu inkonsistenten Daten bei der Aktualisierung der Datenbank, da mein Ansatz die integrierte Logik der SQLiteOpenHelper-Klasse beeinträchtigt. Meine OnCreate -Methode ist leer, weil ich alles im Konstruktor < /p>
mache

Code: Select all

public class DB_SQLite_Helper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "internal_database.db";
public static final int DBVERSION = 1;

public DB_SQLite_Helper(Context context) {
super(context,DATABASE_NAME,null,DBVERSION);
if (!ifDBExists(context)) {
if (!copyDBFromAssets(context)) {
throw new RuntimeException("Failed to Copy Database From Assets Folder");
}
}
mDB = this.getWritableDatabase();
}

SQLiteDatabase mDB;

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {

}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
//Update for the 2nd version
if (oldVersion < 2) {

}
}

////////////////////////////////////////////////////////////////////////////

/*
Copies the database from the assets folder to the apps database folder (with logging)
note databases folder is typically data/data/the_package_name/database
however using getDatabasePath method gets the actual path (should it not be as above)
This method can be significantly reduced one happy that it works.
*/
@SuppressWarnings("IOStreamConstructor")
private boolean copyDBFromAssets(Context context) {
Log.d("CPYDBINFO","Starting attempt to cop database from the assets file.");
String DBPATH = context.getDatabasePath(DATABASE_NAME).getPath();
InputStream is;
OutputStream os;
int buffer_size = 8192;
int length = buffer_size;
long bytes_read = 0;
long bytes_written = 0;
byte[] buffer = new byte[length];

try {

is = context.getAssets().open(DATABASE_NAME);
} catch (IOException e) {
Log.e("CPYDB FAIL - NO ASSET","Failed to open the Asset file " + DATABASE_NAME);
return false;
}

try {
os = new FileOutputStream(DBPATH);
} catch (IOException e) {
Log.e("CPYDB FAIL - OPENDB","Failed to open the Database File at " + DBPATH);
return false;
}
Log.d("CPYDBINFO","Initiating copy from asset file" + DATABASE_NAME + " to " + DBPATH);
while (length >= buffer_size) {
try {
length = is.read(buffer,0,buffer_size);
} catch (IOException e) {
Log.e("CPYDB FAIL - RD ASSET",
"Failed while reading in data from the Asset. " +
bytes_read +
" bytes read successfully."
);
return false;
}
bytes_read = bytes_read + length;
try {
os.write(buffer,0,buffer_size);
} catch (IOException e) {
Log.e("CPYDB FAIL - WR ASSET","failed while writing Database File " +
DBPATH +
". " +
bytes_written +
" bytes written successfully.");
return false;

}
bytes_written = bytes_written + length;
}
Log.d("CPYDBINFO",
"Read " + bytes_read + " bytes. " +
"Wrote " + bytes_written + " bytes."
);
try {
os.flush();
is.close();
os.close();
} catch (IOException e ) {
Log.e("CPYDB FAIL - FINALISING","Failed Finalising Database Copy.  " +
bytes_read +
" bytes read." +
bytes_written +
" bytes written."
);
return false;
}
return true;
}
/*
Checks to see if the database exists if not will create the respective directory (database)
Creating the directory overcomes the NOT FOUND error
*/
private boolean ifDBExists(Context context) {
String dbparent = context.getDatabasePath(DATABASE_NAME).getParent();
File f = context.getDatabasePath(DATABASE_NAME);
if (!f.exists()) {
Log.d("NODB MKDIRS", "Database file not found, making directories."); //

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post