0

我正在尝试从表中更改密码,但出现错误。谁能帮我找到解决方案?

这是我的数据库类代码:

public boolean change(String answer, String newpassword) {
    String sql = "UPDATE "+DATABASE_TABLE +" SET " + KEY_PASSWORD+ " = '"+newpassword+"' WHERE "+KEY_ANSWER+ " = "+answer;
    Cursor cur=db.rawQuery(sql, null);
    if (cur != null) {           
        if(cur.getCount() > 0)
        {
            return true;
        }
    }
    return false;
}

这是我的安卓代码:

if(quest.change(answer, newpassword)){
    Intent login=new Intent(forgot.this, securityLogin.class);
    startActivity(login);
} else{
    Toast.makeText(forgot.this, "wrong answer",Toast.LENGTH_SHORT).show();
}

这是我的日志:

 08-27 13:23:52.735: E/AndroidRuntime(464): FATAL EXCEPTION: main
 08-27 13:23:52.735: E/AndroidRuntime(464): java.lang.IllegalStateException: database not open
 08-27 13:23:52.735: E/AndroidRuntime(464):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1333)
 08-27 13:23:52.735: E/AndroidRuntime(464):     at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1315)
 08-27 13:23:52.735: E/AndroidRuntime(464):     at com.inovawe.inwp.securityDB.change(securityDB.java:163)
 08-27 13:23:52.735: E/AndroidRuntime(464):     at com.inovawe.inwp.forgot$1.onClick(forgot.java:36)
 08-27 13:23:52.735: E/AndroidRuntime(464):     at android.view.View.performClick(View.java:2408)
 08-27 13:23:52.735: E/AndroidRuntime(464):     at android.view.View$PerformClick.run(View.java:8816)
 08-27 13:23:52.735: E/AndroidRuntime(464):     at android.os.Handler.handleCallback(Handler.java:587)
 08-27 13:23:52.735: E/AndroidRuntime(464):     at android.os.Handler.dispatchMessage(Handler.java:92)
 08-27 13:23:52.735: E/AndroidRuntime(464):     at android.os.Looper.loop(Looper.java:123)
 08-27 13:23:52.735: E/AndroidRuntime(464):     at android.app.ActivityThread.main(ActivityThread.java:4627)
 08-27 13:23:52.735: E/AndroidRuntime(464):     at java.lang.reflect.Method.invokeNative(Native Method)
 08-27 13:23:52.735: E/AndroidRuntime(464):     at java.lang.reflect.Method.invoke(Method.java:521)
 08-27 13:23:52.735: E/AndroidRuntime(464):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
 08-27 13:23:52.735: E/AndroidRuntime(464):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
 08-27 13:23:52.735: E/AndroidRuntime(464):     at dalvik.system.NativeStart.main(Native Method)

我得到了答案,下面的代码正在运行

rawQuery("UPDATE "+DATABASE_TABLE +" SET " + KEY_PASSWORD+ " = '"+newpassword+"' WHERE answer=?", new String[]{answer});
4

3 回答 3

0
"08-27 13:23:52.735: E/AndroidRuntime(464): java.lang.IllegalStateException: database not open"

您在代码中的哪个位置打开与数据库的连接?

于 2012-08-27T08:05:26.203 回答
0

您需要在更新之前打开数据库连接。就像是

db.Open();

或者

if (!db.isOpen()) {
   db = getApplicationContext().openOrCreateDatabase(DATABASE_PATH, SQLiteDatabase.OPEN_READWRITE, null);
 }

并记住在工作完成后关闭连接。

于 2012-08-27T08:06:44.847 回答
0

您之前已关闭数据库,db.close()请仅创建一个数据库实例并在所有事务完成后关闭它。

于 2012-08-27T08:07:02.303 回答