Minor fixes

git-svn-id: https://forja.rediris.es/svn/cusl6-swadroid/trunk@59 5bc14d19-1e4b-4ba2-aa50-860af135f48c
This commit is contained in:
Juan Miguel Boyero Corral 2011-11-22 22:04:22 +00:00
parent 4b27a7dc1e
commit 89d2ef7889

View File

@ -63,12 +63,15 @@ public class DataBaseHelper extends SQLiteOpenHelper {
boolean dbExist = checkDataBase(); boolean dbExist = checkDataBase();
if(dbExist){ if(dbExist){
//do nothing - database already exist //do nothing - database already exist
Log.d("createDataBase", "Database already exists.");
}else{ }else{
//By calling this method and empty database will be created into the default system path //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. //of your application so we are gonna be able to overwrite that database with our database.
this.getReadableDatabase(); this.getReadableDatabase();
try { try {
Log.d("createDataBase", "Creating database...");
copyDataBase(); copyDataBase();
Log.d("createDataBase", "Database created successfully.");
} catch (IOException e) { } catch (IOException e) {
throw new Error(context.getString(R.string.errorCopyMsg_DB)); throw new Error(context.getString(R.string.errorCopyMsg_DB));
} }
@ -78,14 +81,19 @@ public class DataBaseHelper extends SQLiteOpenHelper {
/** /**
* Check if the database already exist to avoid re-copying the file each time you open the application. * Check if the database already exist to avoid re-copying the file each time you open the application.
* @return true if it exists, false if it doesn't * @return true if it exists, false if it doesn't
* @throws IOException
*/ */
private boolean checkDataBase(){ private boolean checkDataBase() throws IOException{
SQLiteDatabase checkDB = null; SQLiteDatabase checkDB = null;
try{ try{
Log.d("checkDataBase", "Checking database...");
String myPath = DB_PATH + DB_NAME; String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
Log.d("checkDataBase", "Database already exists.");
}catch(SQLiteException e){ }catch(SQLiteException e){
//database does't exist yet. //database doesn't exist yet.
Log.d("checkDataBase", "Database doesn't exist yet.");
createDataBase();
} }
if(checkDB != null){ if(checkDB != null){
@ -101,28 +109,40 @@ public class DataBaseHelper extends SQLiteOpenHelper {
* @throws IOException * @throws IOException
**/ **/
private void copyDataBase() throws IOException{ private void copyDataBase() throws IOException{
Log.d("copyDataBase", "Copying database to destination...");
//Open your local db as the input stream //Open your local db as the input stream
InputStream myInput = context.getAssets().open(DB_NAME); InputStream myInput = context.getAssets().open(DB_NAME);
// Path to the just created empty db // Path to the just created empty db
String outFileName = DB_PATH + DB_NAME; String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream //Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName); OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile //transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
int length; int length;
while ((length = myInput.read(buffer))>0){ while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length); myOutput.write(buffer, 0, length);
} }
//Close the streams //Close the streams
myOutput.flush(); myOutput.flush();
myOutput.close(); myOutput.close();
myInput.close(); myInput.close();
Log.d("copyDataBase", "Database successfully copied.");
} }
public void openDataBase() throws SQLException{ public void openDataBase() throws SQLException, IOException {
Log.d("openDataBase", "Opening database...");
checkDataBase();
//Open the database //Open the database
String myPath = DB_PATH + DB_NAME; String myPath = DB_PATH + DB_NAME;
swadroidDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE); swadroidDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
Log.d("openDataBase", "Database successfully opened.");
} }
@Override @Override
@ -304,10 +324,10 @@ public class DataBaseHelper extends SQLiteOpenHelper {
String command = "INSERT INTO " String command = "INSERT INTO "
+ Global.DB_TABLE_COURSES + Global.DB_TABLE_COURSES
+ " (_id, name) VALUES (" + " (_id, name) VALUES ("
+ c.getId() + ", " + c.getId() + ", \'"
+ c.getName() + c.getName()
+ ")"; + "\')";
Log.d("DB", command);
swadroidDataBase.execSQL(command, null); swadroidDataBase.execSQL(command, null);
} }
@ -321,9 +341,9 @@ public class DataBaseHelper extends SQLiteOpenHelper {
+ Global.DB_TABLE_NOTICES + Global.DB_TABLE_NOTICES
+ " (_id, timestamp, description) VALUES (" + " (_id, timestamp, description) VALUES ("
+ n.getId() + ", " + n.getId() + ", "
+ n.getTimestamp() + ", " + n.getTimestamp() + ", \'"
+ n.getDescription() + n.getDescription()
+ ")"; + "\')";
swadroidDataBase.execSQL(command, null); swadroidDataBase.execSQL(command, null);
} }
@ -338,11 +358,11 @@ public class DataBaseHelper extends SQLiteOpenHelper {
+ Global.DB_TABLE_STUDENTS + Global.DB_TABLE_STUDENTS
+ " (_id, dni, firstname, surname1, surname2) VALUES (" + " (_id, dni, firstname, surname1, surname2) VALUES ("
+ s.getId() + ", " + s.getId() + ", "
+ s.getDni() + ", " + s.getDni() + ", \'"
+ s.getFirstName() + ", " + s.getFirstName() + "\', \'"
+ s.getSurname1() + ", " + s.getSurname1() + "\', \'"
+ s.getSurname2() + s.getSurname2()
+ ")"; + "\')";
swadroidDataBase.execSQL(command, null); swadroidDataBase.execSQL(command, null);
} }
@ -356,8 +376,8 @@ public class DataBaseHelper extends SQLiteOpenHelper {
String command = "INSERT INTO " String command = "INSERT INTO "
+ Global.DB_TABLE_TEST_ANSWERS + Global.DB_TABLE_TEST_ANSWERS
+ " (_id, answer, correct) VALUES (" + " (_id, answer, correct) VALUES ("
+ a.getId() + ", " + a.getId() + ", \'"
+ a.getAnswer() + ", " + a.getAnswer() + "\', "
+ parseBoolInt(a.getCorrect()) + parseBoolInt(a.getCorrect())
+ ")"; + ")";
@ -375,8 +395,8 @@ public class DataBaseHelper extends SQLiteOpenHelper {
+ " (_id, anstype, numhits, question, score, shuffle) VALUES (" + " (_id, anstype, numhits, question, score, shuffle) VALUES ("
+ q.getId() + ", " + q.getId() + ", "
+ q.getAnstype() + ", " + q.getAnstype() + ", "
+ q.getNumhits() + ", " + q.getNumhits() + ", \'"
+ q.getQuestion() + ", " + q.getQuestion() + "\', "
+ q.getScore() + ", " + q.getScore() + ", "
+ parseBoolString(q.getShuffle()) + parseBoolString(q.getShuffle())
+ ")"; + ")";
@ -393,10 +413,10 @@ public class DataBaseHelper extends SQLiteOpenHelper {
String command = "INSERT INTO " String command = "INSERT INTO "
+ Global.DB_TABLE_MSG_CONTENT + Global.DB_TABLE_MSG_CONTENT
+ " (msgcod, subject, content) VALUES (" + " (msgcod, subject, content) VALUES ("
+ m.getId() + ", " + m.getId() + ", \'"
+ m.getSubject() + ", " + m.getSubject() + "\', \'"
+ m.getContent() + m.getContent()
+ ")"; + "\')";
swadroidDataBase.execSQL(command, null); swadroidDataBase.execSQL(command, null);
} }