I have a function which is supposed to add a new record to the SQLite database. This function will then call a function to return an int to a variable, then the rest of the code is skipped and it goes straight into the finally statement.
Below is the method.
SQLiteDatabase myDb = null;
if (type.equals("Website"))
{
details = formatUrl(details);
}
try
{
myDb = context.openOrCreateDatabase("PasswordManager", Context.MODE_PRIVATE, null);
int rowId = common.getNextID("password");
//ALL OF THIS CODE IS SKIPPED
ContentValues cv = new ContentValues();
cv.put("id", rowId);
cv.put("category", category);
cv.put("company", Encryption.encrypt(company));
cv.put("loginAction", Encryption.encrypt(details));
cv.put("username", Encryption.encrypt(username));
cv.put("password", Encryption.encrypt(password));
cv.put("type", type);
cv.put("appName", "N/A");
myDb.insert("password", null, cv);
}
catch (SQLiteException ex)
{
common.showBasicAlertDialog("Something has gone wrong.\n\nWe will fix this as soon as we can", false);
Log.e("Database Errror", ex.toString());
return false;
}
catch (SQLException ex)
{
common.showBasicAlertDialog("Something has gone wrong.\n\nWe will fix this as soon as we can", false);
Log.e("SQL Error", ex.toString());
return false;
}
finally
{
//IT GOES STRAIGHT INTO THIS CODE AFTER THE GETNEXTID METHOD RETURNS
if (myDb.isOpen())
{
myDb.close();
return true;
}
}
return false;
Below is the code for the getNextId() function
public int getNextID(String table)
{
int nextID = 1;
Cursor cursor = null;
SQLiteDatabase myDB = null;
try
{
myDB = context.openOrCreateDatabase("PasswordManager", Context.MODE_PRIVATE, null);
cursor = myDB.rawQuery("SELECT id FROM "+table+" ORDER BY id DESC LIMIT 1", null);
if (cursor != null)
{
cursor.moveToFirst();
nextID = cursor.getInt(0) + 1;
}
}
catch (SQLiteException ex)
{
Log.d("GetNextID", ex.toString());
nextID = -1;
}
finally
{
if (myDB.isOpen())
{
myDB.close();
}
if (!cursor.isClosed())
{
cursor.close();
}
}
return nextID;
}
I don’t understand the content values have been skipped and it goes straight into the finally.
Perhaps some exception other than
SQLExceptionandSQLiteExceptionhave been thrown? If you putcatch(Exception x) {...}you will probably see.