我的Sqlite Android出错了。我有这样的代码:
public int UpdatePhotoUser(String foto_user, int metode) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
int update = 0;
if (metode == 1) {
values.put(Variabel.KEY_USER_PHOTO, foto_user);
try {
update = db.update(
Variabel.TABLE_USER,
values, null, null);
} catch (android.database.sqlite.SQLiteException e) {
}
} else if (metode == 2 && getUserDetails(Variabel.KEY_USER_PHOTO).equals(foto_user)) {
values.put(Variabel.KEY_USER_PHOTO, "");
try {
update = db.update(
Variabel.TABLE_USER,
values, null, null);
} catch (android.database.sqlite.SQLiteException e) {
}
}
db.close();
return update;
}
我在最后一行有错误:
update = db.update(
Variabel.TABLE_USER,
values, null, null);
这是恐怖:
Java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/com.ad.kamardagang.newseditor/databases/DB_KAMAR_DAGANG
at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
at android.database.sqlite.SQLiteDatabase.updateWithOnConflict(SQLiteDatabase.java:1545)
at android.database.sqlite.SQLiteDatabase.update(SQLiteDatabase.java:1522)
at com.ad.kamardagang.newseditor.database_content.DatabaseHandler.UpdatePhotoUser(DatabaseHandler.java:613)
at com.ad.kamardagang.newseditor.fragments.PhotoUserFragment.removeRespone(PhotoUserFragment.java:985)
at com.ad.kamardagang.newseditor.fragments.PhotoUserFragment.access$600(PhotoUserFragment.java:62)
at com.ad.kamardagang.newseditor.fragments.PhotoUserFragment$12.onResponse(PhotoUserFragment.java:879)
at com.ad.kamardagang.newseditor.fragments.PhotoUserFragment$12.onResponse(PhotoUserFragment.java:875)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
编辑: 我打电话给:
db = new DatabaseHandler(getActivity());
db.UpdatePhotoUser("http://a.png",1);
当。我第一次调用metode UpdatePhotoUser(String foto_user,int metode),但在第二次上调用其错误。那怎么解决呢?
答案 0 :(得分:2)
问题是您在方法结束时关闭了数据库:
db.close();
你不需要这样做。当应用程序不再使用时,数据库将被关闭。
无论如何,有更好的方法来管理数据库。搜索有关 ContentProvider 类的教程,有一些有用的资源。
答案 1 :(得分:0)
我认为您在更新前已关闭 db 对象。
答案 2 :(得分:0)
在第一次通话结束时关闭对象。
db.close();
您似乎使用个人方法来获取SQLiteDatabase
SQLiteDatabase db = this.getWritableDatabase();
尝试使用单例并测试实例是否已关闭:
private Singleton()
{}
private static Singleton INSTANCE = new Singleton();
public static Singleton getInstance()
{
//Some code
if(!INSTANCE.isOpen()){
//Some code
}
}
答案 3 :(得分:0)
如果有人遇到类似问题,请尝试解决问题
if (db == null || !db.isOpen())
{
dbHelper = new MySQLiteHelper(this); //u can omit this but need to have an extra check dbHelper!=null
db = dbHelper.getWritableDatabase();
}