Ok, sorry.. I didn't want to be too verbose..
This is the first part of the openHandler (the one which is in charge of
opening the db and creating it)..

public class CurrencyStorageHelper extends SQLiteOpenHelper {
private static final String TAG = "CurrencyStorageHelper";
private static final int DATABASE_VERSION = 1;
 public static final String DATABASE_NAME = "easycurrencyconverter.db";
public static final String COL_ID = "_id";
 public static final String COL_CURRENCY_CODE = "currency_code";
public static final String COL_CURRENCY_VALUE = "currency_value";
public static final String COL_LAST_UPDATE = "last_update";
public static final String COL_LAST_DOWNLOADED = "last_downloaded";
public static final String COL_IS_TARGET_CURRENCY = "is_target_currency";
public static final String COL_CURRENCY_POSITION = "position";
 public static final String TABLE_DEFAULT_CURRENCIES = "defaultcurrencies";
public static final String TABLE_CURRENCIES = "currencyhistory";
public static final String TABLE_CURRENCY_DEFINITION = "currencydefinition";

public static final String COL_DEFINITION_CODE = "code";
public static final String COL_DEFINITION_FULL_NAME = "full_name";


 private static final String DATABASE_CREATE_1 =
"CREATE TABLE " + TABLE_CURRENCIES + " (" + COL_ID + " INTEGER PRIMARY KEY
AUTOINCREMENT " +
", " + COL_CURRENCY_CODE + " TEXT NOT NULL" +
", " + COL_CURRENCY_VALUE + " REAL NOT NULL" +
", " + COL_LAST_UPDATE + " REAL NOT NULL" +
", " + COL_LAST_DOWNLOADED + " REAL NOT NULL); ";
        private static final String DATABASE_CREATE_2 =
"CREATE TABLE " + TABLE_DEFAULT_CURRENCIES +
           " (" + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT " +
   ", " + COL_CURRENCY_CODE + " TEXT NOT NULL" +
   ", " + COL_CURRENCY_VALUE + " REAL NOT NULL" +
   ", " + COL_CURRENCY_POSITION + " INTEGER NOT NULL" +
   ", " + COL_LAST_UPDATE + " REAL NOT NULL" +
   ", " + COL_LAST_DOWNLOADED + " REAL NOT NULL" +
   ", " + COL_IS_TARGET_CURRENCY + " NUMERIC ); ";
         private static final String DATABASE_CREATE_3 =
 "CREATE TABLE " + TABLE_CURRENCY_DEFINITION +
              " (" + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT " +
      ", " + COL_DEFINITION_CODE + " TEXT NOT NULL" +
      ", " + COL_DEFINITION_FULL_NAME + " TEXT NOT NULL); ";
 private static final String DATABASE_DROP =
           "DROP TABLE IF EXISTS " + TABLE_CURRENCIES + "; " +
   "DROP TABLE IF EXISTS " + TABLE_DEFAULT_CURRENCIES + ";" +
   "DROP TABLE IF EXISTS " + TABLE_CURRENCY_DEFINITION + ";";
 public CurrencyStorageHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onOpen(SQLiteDatabase db) {
super.onOpen(db);
Log.d(TAG, "Opening the database... " + db.getPath() + " version " +
db.getVersion());
db.setLockingEnabled(true);
}
 @Override
public void onCreate(SQLiteDatabase database) {
Log.d(TAG, "Call to onCreate");

Log.d(TAG, "Creating table..." + DATABASE_CREATE_1);
database.execSQL(DATABASE_CREATE_1);
 Log.d(TAG, "Creating table..." + DATABASE_CREATE_2);
database.execSQL(DATABASE_CREATE_2);

Log.d(TAG, "Creating table..." + DATABASE_CREATE_3);
database.execSQL(DATABASE_CREATE_3);
 insertDefinitions(database);
}

@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int
newVersion) {
database.execSQL(DATABASE_DROP);
 onCreate(database);
}

/**
 * Create the default values to be shown to the user main activity when the
application
 * is ran for the first time and no currencies have been downloaded yet.
 * @param database
 */
private void insertInitialCurrencies(SQLiteDatabase database){

int i = 1;
//TODO The fact that USD is the target currency is arbitrary. Maybe we
should read user's
//locale to get her currency
insertDefaultCurrency(database, "USD", "U.S. Dollar", 1, i++, true, (new
Date()).getTime(), (new Date()).getTime(), true, false);
insertDefaultCurrency(database, "EUR", "Euro", 1, i++, false, (new
Date()).getTime(), (new Date()).getTime(), false, false);
insertDefaultCurrency(database, "GBP", "British Pound", 1, i++, false, (new
Date()).getTime(), (new Date()).getTime(), false, false);
insertDefaultCurrency(database, "JPY", "Japanese Yen", 1, i++, false, (new
Date()).getTime(), (new Date()).getTime(), false, false);
insertDefaultCurrency(database, "CNY", "Chinese Yuan", 1, i++, false, (new
Date()).getTime(), (new Date()).getTime(), false, false);
insertDefaultCurrency(database, "CHF", "Swiss Franc", 1, i++, false, (new
Date()).getTime(), (new Date()).getTime(), false, true);

}
 /**
 * Insert a new currency value which will be shown on the user main
activity.
 */
private void insertDefaultCurrency(SQLiteDatabase database,
String currencyCode,
String currencyFullName,
double value,
int position,
boolean isTargetCurrency,
long lastModifiedDate,
long lastDownloadedDate,
boolean startTransaction,
boolean endTransaction){
ContentValues vals = new ContentValues();
vals.put(COL_CURRENCY_CODE, currencyCode);
vals.put(COL_CURRENCY_VALUE, value);
vals.put(COL_LAST_DOWNLOADED, lastDownloadedDate);
vals.put(COL_LAST_UPDATE, lastModifiedDate);
vals.put(COL_IS_TARGET_CURRENCY, (isTargetCurrency? 1 : 0));
vals.put(COL_CURRENCY_POSITION, position);
 if (startTransaction){
database.beginTransaction();
}
Log.d(TAG, "Adding default currency..." + currencyCode);
database.insertOrThrow(TABLE_DEFAULT_CURRENCIES, null, vals);
 if (endTransaction){
database.endTransaction();
}
}

No exception gets thrown so it looks like the record have been inserted, but
when I try to run this query:

private Cursor queryCurrentCurrencies(enumQueryCurrentCurrencies rule){
 String SQL = "SELECT v." + CurrencyStorageHelper.COL_ID
 + ", v." + CurrencyStorageHelper.COL_CURRENCY_CODE
 + ", d." + CurrencyStorageHelper.COL_DEFINITION_FULL_NAME
 + ", v." + CurrencyStorageHelper.COL_CURRENCY_VALUE
 + " FROM " + CurrencyStorageHelper.TABLE_DEFAULT_CURRENCIES + " v "
 + " JOIN " + CurrencyStorageHelper.TABLE_CURRENCY_DEFINITION + " d "
 + " ON v." + CurrencyStorageHelper.COL_CURRENCY_CODE
 + " =  d." + CurrencyStorageHelper.COL_DEFINITION_CODE;

if (rule.compareTo(enumQueryCurrentCurrencies.TARGET) == 0){
SQL += " WHERE v." + CurrencyStorageHelper.COL_IS_TARGET_CURRENCY + " = 1";
}
else if (rule.compareTo(enumQueryCurrentCurrencies.CONVERTED) == 0){
SQL += " WHERE v." + CurrencyStorageHelper.COL_IS_TARGET_CURRENCY + " = 0";
}
 SQL += ";";
 return this.db.rawQuery(SQL, new String[] {});
}

obviously I get an exception and indeed, opening the DB using a DB
management tool, the db is completely empty (with the  exception of the
default android_metadata table.

I'm sure I'm doing something silly somewhere... I hope you will spot the
missing stone :)

Thanks a log for your precious time!!

Nico

2011/8/22 Drezden <[email protected]>

> If possible post a complete class file for your database helper, it's
> really hard to know what's happening when you show random parts from
> all of your classes.
>
> On Aug 22, 4:07 pm, Nico Balestra <[email protected]> wrote:
> > Ok guys.. I've tried and tried in the hope not to be forced to bother
> you.
> > but I have to :)
> > I'm trying to do a simple thing.. creating a DB when my activity first
> > starts:
> > @Override
> >     public void onCreate(Bundle savedInstanceState) {
> >
> >         currenciesDB = new CurrenciesDB(this);
> >         currenciesDB.open();
> >
> >  }
> >
> > then in my CurrenciesDB constructor/open:
> >
> > public CurrenciesDB(Context mCtx) {
> > this.ctx = mCtx;}
> >
> > public CurrenciesDB open() throws SQLException {
> > this.dbHelper = new CurrencyStorageHelper(this.ctx);
> > this.db = dbHelper.getWritableDatabase();
> >  return this;
> >
> > }
> >
> > and, as you might reckon, my CurrencyStorageHelper class extends the
> > SQLOpenHelper class and specifically:
> > public CurrencyStorageHelper(Context context) {
> > super(context, DATABASE_NAME, null, DATABASE_VERSION);
> >
> > }
> >
> > @Override
> > public void onOpen(SQLiteDatabase db) {
> > super.onOpen(db);
> > Log.d(TAG, "Opening the database... " + db.getPath() + " version " +
> > db.getVersion());
> > db.setLockingEnabled(true);}
> >
> >  @Override
> > public void onCreate(SQLiteDatabase database) {
> > database.execSQL(DATABASE_CREATE_1);
> >  Log.d(TAG, "Creating table..." + DATABASE_CREATE_2);
> > database.execSQL(DATABASE_CREATE_2);
> > Log.d(TAG, "Creating table..." + DATABASE_CREATE_3);
> > database.execSQL(DATABASE_CREATE_3);
> >  insertDefinitions(database);
> > insertInitialCurrencies(database);
> >
> > }
> >
> > What is happening here is that despite launcing the "CREATE TABLE"
> > statements, nothing gets created on the database. After those statements
> I
> > even insert records using SQLiteDatabase.insert. But nothing happens.. no
> > expections and no records get created.
> > Afterward, when I try to query the DB, I get an option saying that the
> table
> > I'm trying to query doesn't (obviously) exists.
> > I'm not a newbie developer and I've tried to debug my app as much as I
> > can/know but I have only these clues:
> > 1. During the debug process, the instance of SQLiteDatabase has a
> > mStrackTrace private attribute set to DatabaseObjectNotClosedException,
> but
> > obviously no cursor has been opened.. yet.
> > 2. The db version gets set to zero :(
> > 3. Same behaviour using the Emulator and my Samsunga Galaxy S2 2.3.3
> (hence
> > no wrong configuration of the emulator).
> >
> > If you had patient to go through the entire email I hope you have even
> one
> > single clue on why this is happening (maybe you experienced the same
> > problem?? ) !!
> >
> > Log.d("Nico", "THANK YOU");
> >
> > Nico
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to