I need to Import and Export my SQlite database. I have my ListAdapter and SQliteHelper class. I have written code by refering some articles but not able to adapt it as per my requirement. I am having ListAdapter
class, SQLiteHelper
class, DbExportImport
class, Getting error while adapter.open()
and adapter.close()
. Most probably my DbExportImport
class has errors. I am not able to figure out/Understand that code.
Can someone breifly explain whats happening here. Someone may get help from this.
This is DbExportImport
Class
/** Imports the file at IMPORT_FILE **/
protected static boolean importIntoDb(Context ctx){
if( ! SdIsPresent() ) return false;
File importFile = IMPORT_FILE;
if( ! checkDbIsValid(importFile) ) return false;
try{
SQLiteDatabase sqlDb = SQLiteDatabase.openDatabase
(importFile.getPath(), null, SQLiteDatabase.OPEN_READONLY);
Cursor cursor = sqlDb.query(true, DATABASE_TABLE,
null, null, null, null, null, null, null
);
DbAdapter dbAdapter = new DbAdapter(ctx);
dbAdapter.open();
final int titleColumn = cursor.getColumnIndexOrThrow("title");
final int timestampColumn = cursor.getColumnIndexOrThrow("timestamp");
// Adds all items in cursor to current database
cursor.moveToPosition(-1);
while(cursor.moveToNext()){
dbAdapter.createQuote(
cursor.getString(titleColumn),
cursor.getString(timestampColumn)
);
}
sqlDb.close();
cursor.close();
dbAdapter.close();
} catch( Exception e ){
e.printStackTrace();
return false;
}
return true;
}
/** Given an SQLite database file, this checks if the file
* is a valid SQLite database and that it contains all the
* columns represented by DbAdapter.ALL_COLUMN_KEYS **/
protected static boolean checkDbIsValid( File db ){
try{
SQLiteDatabase sqlDb = SQLiteDatabase.openDatabase
(db.getPath(), null, SQLiteDatabase.OPEN_READONLY);
Cursor cursor = sqlDb.query(true, DATABASE_TABLE,
null, null, null, null, null, null, null
);
// ALL_COLUMN_KEYS should be an array of keys of essential columns.
// Throws exception if any column is missing
for( String s : DbAdapter.ALL_COLUMN_KEYS ){
cursor.getColumnIndexOrThrow(s);
}
sqlDb.close();
cursor.close();
} //catching all exceptions here
return true;
}
I think the answer is, use neither but perhaps adapt DbAdapter to suit.
It would appear that the DbAdapter is not an Adapter for display purposes but is adapting/converting/extracting the the data from the imported database to be suitable for local use.
ListAdapter is an adapter for display purposes not for the conversion
In short the method importIntoDb
:-
checkDbIsValid
which
SQLiteDatabase sqlDb = SQLiteDatabase.openDatabase(db.getPath(), null, SQLiteDatabase.OPEN_READONLY);
- Opens the file as an SQLite database, if the file is not an SQLite database, as understood by SQLite then the exception is trapped and the method returns false. This is OK/Suitable as it's generic.Cursor cursor = sqlDb.query(true, DATABASE_TABLE, null, null, null, null, null, null, null);
- Extracts a Cursor with all columns and all rows (except duplicates) from the table as per DATABASE_TABLE, whatever that is. This requires that DATABASE_TABLE, wherever set, is set to a table that exists in the imported database.for( String s : DbAdapter.ALL_COLUMN_KEYS ){
cursor.getColumnIndexOrThrow(s);
}
- Checks to see if the Columns in the Cursor are all found in the String array that is class variable of the DbAdapter
class. This REQUIRES the DbAdapter class. So this would need to be omitted or adjusted.true
or false
the latter indicating that the Database is invalid.title
and timestamp
. DbAdapter
's createQuote
method utilising the data extracted from the title
and timestamp
columns. As such this would only work if the said columns existed in the source and an instance of the DbAdapter class existed. ....
To adapt DbAdapter you would likely have to continue looking at the code you have copied and determining if it is suitable or not for your purpose.