aboutsummaryrefslogtreecommitdiff
path: root/src/net/cbaines/suma/DatabaseHelper.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/net/cbaines/suma/DatabaseHelper.java')
-rw-r--r--src/net/cbaines/suma/DatabaseHelper.java110
1 files changed, 6 insertions, 104 deletions
diff --git a/src/net/cbaines/suma/DatabaseHelper.java b/src/net/cbaines/suma/DatabaseHelper.java
index 21ee99b..d039518 100644
--- a/src/net/cbaines/suma/DatabaseHelper.java
+++ b/src/net/cbaines/suma/DatabaseHelper.java
@@ -19,11 +19,6 @@
package net.cbaines.suma;
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
import java.sql.SQLException;
import android.content.Context;
@@ -35,12 +30,7 @@ import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
-public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
-
- private static final String DATABASE_PATH = "/data/data/net.cbaines.suma/databases/";
- private static final String DATABASE_NAME = "data.db";
-
- private static final int DATABASE_VERSION = 41;
+public class DatabaseHelper extends OrmLiteSqliteOpenHelper implements Preferences {
private static final String TAG = "DatabaseHelper";
@@ -52,24 +42,25 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
private Dao<Site, String> siteDao = null;
private Dao<Bus, Integer> busDao = null;
- private Context context;
+ volatile boolean doneUpgrade = false;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION, R.raw.ormlite_config);
Log.i(TAG, "Database Helper created");
- this.context = context;
}
@Override
public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
try {
- Log.i(DatabaseHelper.class.getName(), "onCreate");
+ Log.i(TAG, "onCreate");
+
TableUtils.createTable(connectionSource, Building.class);
TableUtils.createTable(connectionSource, BusStop.class);
TableUtils.createTable(connectionSource, BusRoute.class);
TableUtils.createTable(connectionSource, RouteStop.class);
TableUtils.createTable(connectionSource, Site.class);
TableUtils.createTable(connectionSource, Bus.class);
+
} catch (SQLException e) {
Log.e(DatabaseHelper.class.getName(), "Can't create database", e);
throw new RuntimeException(e);
@@ -78,13 +69,8 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
@Override
public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
- try {
- Log.i(DatabaseHelper.class.getName(), "onUpgrade");
- copyDataBase();
- } catch (IOException e) {
- e.printStackTrace();
- }
+ Log.i(TAG, "onUpgrade");
}
@@ -153,90 +139,6 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
*
* @return true if it exists, false if it doesn't
*/
- public boolean checkDataBase() {
- Log.i(TAG, "Check database");
-
- /*
- * SQLiteDatabase checkDB = null;
- *
- * try { String myPath = DATABASE_PATH + DATABASE_NAME; checkDB = SQLiteDatabase.openDatabase(myPath, null,
- * SQLiteDatabase.OPEN_READONLY); } catch (SQLiteException e) {
- *
- * // database does't exist yet.
- *
- * }
- *
- * if (checkDB != null) {
- *
- * checkDB.close();
- *
- * }
- *
- * Log.i(TAG, "Finished checking database"); return checkDB != null ? true : false;
- */
-
- File dbFile = new File(DATABASE_PATH + DATABASE_NAME);
- return dbFile.exists();
- }
-
- /**
- * Copies your database from your local assets-folder to the just created empty database in the system folder, from where it
- * can be accessed and handled. This is done by transfering bytestream.
- * */
- public void copyDataBase() throws IOException {
- Log.i(TAG, "Begining copy database");
-
- InputStream myInput = context.getAssets().open(DATABASE_NAME);
-
- // Path to the just created empty db
- String outFileName = DATABASE_PATH + DATABASE_NAME;
-
- File database = new File(outFileName);
- if (database.exists()) {
- database.delete();
- }
-
- // 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 = myInput.read(buffer)) > 0) {
- myOutput.write(buffer, 0, length);
- }
-
- // Close the streams
- myOutput.flush();
- myOutput.close();
- myInput.close();
-
- // getWritableDatabase().close();
-
- Log.i(TAG, "Finished copying db");
-
- }
-
- /**
- * 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 {
-
- try {
- Log.i(TAG, "Copy database");
- copyDataBase();
- } catch (IOException e) {
- throw new Error("Error copying database");
- }
- }
-
- }
/**
* Close the database connections and clear any cached DAOs.