0

我有一个应该向 SQLite 数据库添加新记录的函数。然后,该函数将调用一个函数以将一个 int 返回给一个变量,然后跳过其余代码并直接进入 finally 语句。

下面是方法。

    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;

下面是 getNextId() 函数的代码

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;
    }

我不明白内容值已被跳过,它直接进入finally。

4

2 回答 2

2

SQLException也许除了和之外的一些异常SQLiteException已经被抛出?如果你把catch(Exception x) {...}你可能会看到。

于 2012-09-27T18:15:43.493 回答
0

首先尝试移动线路。

myDb = context.openOrCreateDatabase("PasswordManager", Context.MODE_PRIVATE, null);

线以下

 int rowId = common.getNextID("password");

在你的方法中。或者更好的方法是将 myDb 作为参数添加到您的 getNextID 函数中,这样您就可以使用相同的数据库引用,然后不要在该函数中关闭数据库。

添加catch(Exception x)到您的例外。

然后最后在您的方法中放置一个断点getNextID,并准确找出它在哪一行中断。

干杯

于 2012-09-27T18:26:14.600 回答