看看我的DbHelper课程。我认为db.close()
命令会在命令中产生一些错误
在我使用的每种方法getReadable
或GetWritable
我尝试this.close();
这是我的DbHelper
:
public class DbHelper extends SQLiteOpenHelper{
private SQLiteDatabase db;
public DbHelper(Context context) {
super(context, "shareholders.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
this.db = db;
String sql = "CREATE TABLE IF NOT EXISTS news (id integer,title text,description text,sDate text)";
db.execSQL(sql);
} catch (Exception e) {
xLog.error(e.getMessage());
}
ContentValues cv = new ContentValues();
cv.put("name", "Username");
cv.put("value", "default");
db.insert("settings", null, cv);
cv.clear();
cv.put("name", "Password");
cv.put("value", "default");
db.insert("settings", null, cv);
cv.clear();
cv.put("name", "PersonId");
cv.put("value", "default");
db.insert("settings", null, cv);
cv.clear();
cv.put("name", "picture");
cv.put("value", "");
db.insert("settings", null, cv);
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
public long insert(String table,ContentValues cv){
SQLiteDatabase mydb =this.getWritableDatabase();
long result=-1;
try {
result = mydb.insert(table,null, cv);
}catch (Exception e) {
xLog.error(e.getMessage());
}
this.close();
return result;
}
public Cursor selectAll(String table){
SQLiteDatabase mydb =this.getReadableDatabase();
String sql = "SELECT * FROM "+table;
xLog.info(sql);
Cursor result=null;
try {
result = mydb.rawQuery(sql, null);
} catch (Exception e) {
xLog.error(e.getMessage());
}
this.close();
return result;
}
public Cursor select(String table,String where){
SQLiteDatabase mydb =this.getReadableDatabase();
String sql = "SELECT * FROM "+table+" WHERE "+where;
xLog.info(sql);
Cursor result=null;
try {
result = mydb.rawQuery("SELECT * FROM "+table+" WHERE "+where, null);
} catch (Exception e) {
xLog.error(e.getMessage());
}
this.close();
return result;
}
public long delete(String table,String condition){
SQLiteDatabase mydb =this.getWritableDatabase();
long result = -1;
try {
result = mydb.delete(table, condition, null);
} catch (Exception e) {
xLog.error(e.getMessage());
}
this.close();
return result;
}
protected long empty(String table){
SQLiteDatabase mydb =this.getWritableDatabase();
long result = -1;
try {
result = mydb.delete(table, "", null);
} catch (Exception e) {
xLog.error(e.getMessage());
}
this.close();
return result;
}
public long update(String table,ContentValues cv,String condition){
SQLiteDatabase mydb =this.getWritableDatabase();
long result = -1;
try {
result = mydb.update(table, cv, condition, null);
} catch (Exception e) {
xLog.error(e.getMessage());
}
this.close();
return result;
}
protected void drop(String table){
//TODO Produces a damn error!
db.execSQL("DROP TABLE IF EXISTS "+table);
}
}
这是我的日志猫:
11-26 13:12:20.738: W/System.err(2238): org.xmlpull.v1.XmlPullParserException: unexpected type (position:END_DOCUMENT null@1:0 in java.io.InputStreamReader@43e80ca8)
11-26 13:12:20.738: W/System.err(2238): at org.kxml2.io.KXmlParser.exception(KXmlParser.java:273)
11-26 13:12:20.738: W/System.err(2238): at org.kxml2.io.KXmlParser.nextTag(KXmlParser.java:1420)
11-26 13:12:20.738: W/System.err(2238): at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:126)
11-26 13:12:20.738: W/System.err(2238): at org.ksoap2.transport.Transport.parseResponse(Transport.java:96)
11-26 13:12:20.738: W/System.err(2238): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:189)
11-26 13:12:20.745: W/System.err(2238): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:95)
11-26 13:12:20.745: W/System.err(2238): at ClassLibrary.WebService.CallMethod(WebService.java:49)
11-26 13:12:20.745: W/System.err(2238): at ClassLibrary.AsyncCallWs.doInBackground(AsyncCallWs.java:64)
11-26 13:12:20.745: W/System.err(2238): at ClassLibrary.AsyncCallWs.doInBackground(AsyncCallWs.java:1)
11-26 13:12:20.745: W/System.err(2238): at android.os.AsyncTask$2.call(AsyncTask.java:185)
11-26 13:12:20.745: W/System.err(2238): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
11-26 13:12:20.745: W/System.err(2238): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-26 13:12:20.745: W/System.err(2238): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
11-26 13:12:20.745: W/System.err(2238): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
11-26 13:12:20.745: W/System.err(2238): at java.lang.Thread.run(Thread.java:1096)
答案 0 :(得分:2)
该异常与代码无关,但存在问题:
this.db = db;
您正在存储对{1}}的引用,例如您自己没有获得SQLiteDatabase
或getReadableDatabase()
。
getWritableDatabase()
这里使用的存储引用指向可能已关闭的数据库连接。在这里使用protected void drop(String table){
//TODO Produces a damn error!
db.execSQL("DROP TABLE IF EXISTS "+table);
}
来获取db引用。另外,删除getWritableDatabase()
成员变量。你无论如何都不需要它。
内部SQLiteDatabase
个连接被引用计算。如果数据库已打开,则再次打开它只会增加现有连接的引用计数。调用SQLiteDatabase
将减少引用计数器,一旦它达到零,数据库就会关闭。
传递给close()
的数据库引用由Android数据库框架打开和关闭。