我正在尝试从表中更改密码,我收到错误。任何人都可以帮我解决问题吗?
这是我的数据库类代码:
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;
}
这是我的android代码:
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();
}
这是我的logcat:
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});
答案 0 :(得分:0)
"08-27 13:23:52.735: E/AndroidRuntime(464): java.lang.IllegalStateException: database not open"
您的代码中的哪个位置打开了与数据库的连接?
答案 1 :(得分:0)
您需要在更新前打开数据库连接。像
这样的东西db.Open();
或
if (!db.isOpen()) {
db = getApplicationContext().openOrCreateDatabase(DATABASE_PATH, SQLiteDatabase.OPEN_READWRITE, null);
}
请记住在工作完成后关闭连接。
答案 2 :(得分:0)
您之前使用db.close()
关闭了数据库,请创建一个数据库实例,并在完成所有交易后将其关闭。