1 package net.sf.openrocket.android.db;
\r
3 import android.content.Context;
\r
4 import android.database.SQLException;
\r
5 import android.database.sqlite.SQLiteDatabase;
\r
6 import android.database.sqlite.SQLiteOpenHelper;
\r
7 import android.util.Log;
\r
9 public class DbAdapter {
\r
11 private static final String TAG = "DbAdapter";
\r
12 private DatabaseHelper mDbHelper;
\r
13 private SQLiteDatabase mDb;
\r
15 private static final String DATABASE_NAME = "rocketflightnotebook";
\r
16 private static final int DATABASE_VERSION = 2;
\r
18 private final Context mCtx;
\r
20 private MotorDao motorDao;
\r
22 public MotorDao getMotorDao() {
\r
26 private class DatabaseHelper extends SQLiteOpenHelper {
\r
27 DatabaseHelper(Context context) {
\r
28 super(context, DATABASE_NAME, null, DATABASE_VERSION);
\r
32 public void onCreate(SQLiteDatabase db) {
\r
33 executeSQL( db, MotorDao.create());
\r
37 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
\r
38 Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
\r
39 + newVersion + ", which will destroy all old data");
\r
40 executeSQL(db, MotorDao.update(oldVersion, newVersion));
\r
43 private void executeSQL( SQLiteDatabase db, String[] sqls ) {
\r
44 for(String s: sqls ) {
\r
52 * Constructor - takes the context to allow the database to be
\r
55 * @param ctx the Context within which to work
\r
57 public DbAdapter(Context ctx) {
\r
62 * Open the database. If it cannot be opened, try to create a new
\r
63 * instance of the database. If it cannot be created, throw an exception to
\r
64 * signal the failure
\r
66 * @return this (self reference, allowing this to be chained in an
\r
67 * initialization call)
\r
68 * @throws SQLException if the database could be neither opened or created
\r
70 public DbAdapter open() throws SQLException {
\r
71 mDbHelper = new DatabaseHelper(mCtx);
\r
72 mDb = mDbHelper.getWritableDatabase();
\r
73 motorDao = new MotorDao(mDb);
\r
77 public void close() {
\r