数据库在android 2.x中不起作用

时间:2013-11-28 04:44:48

标签: android sqliteopenhelper android-2.0-eclair

当我试图在android 2.x中运行我的项目并在数据库上执行sqlcommands时,一个错误说明“从未在数据库上显式调用close()......”。 但它在android 4.x中工作正常。 我想我应该在我的dbhelper中打开和关闭数据库,但我不知道如何在这个文件中执行它。 这是我的DbHelper:

public class DbHelper extends SQLiteOpenHelper{ 


    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);
            sql = "CREATE TABLE IF NOT EXISTS cities (id integer,name text)";
            db.execSQL(sql);
            sql = "CREATE TABLE IF NOT EXISTS lawyers (id integer,fullName text,mobile text)";
            db.execSQL(sql);
            sql = "CREATE TABLE IF NOT EXISTS persons (id integer,code text,fullName text,father text,birthDate text,shsh text,nationalCode text,city text,postalCode text,email text,homeTel text,workTel text,mobile text,homeAddress text,workAddress text)";
            db.execSQL(sql);
            sql = "CREATE TABLE IF NOT EXISTS settings (name text,value text)";
            db.execSQL(sql);
            sql = "CREATE TABLE IF NOT EXISTS messages (id integer,senderId integer,receiverId integer,SenderName text,sDate text,type text,title text, body text,openned text,sent integer)";
            db.execSQL(sql);
            sql = "CREATE TABLE IF NOT EXISTS financials (id integer,sDate text,mDate text,description text,price integer)";
            db.execSQL(sql);
            sql = "CREATE TABLE IF NOT EXISTS sellRequests (id integer,sDate text,mDate text,description text,shareCount integer)";
            db.execSQL(sql);
            sql = "CREATE TABLE IF NOT EXISTS buyRequests (id integer,sDate text,mDate text,description text,shareCount integer)";
            db.execSQL(sql);
            sql = "CREATE TABLE IF NOT EXISTS shares (id text,type text,sDate text,status text,count integer)";
            db.execSQL(sql);
            sql = "CREATE TABLE IF NOT EXISTS android_metadata (locale 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);

        cv.clear();
        cv.put("locale", "en_US");
        db.insert("android_metadata", null, cv);



    }

    @Override
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
        // TODO Auto-generated method stub

    }

    public long insert(String table,ContentValues cv){
        mydb =this.getWritableDatabase();
        long result=-1;
        try {
            result = mydb.insert(table,null, cv);
            }catch (Exception e) {
            xLog.error(e.getMessage());
        }
        finally{
//          mydb.close();
        }
        return result;
    }

    public Cursor selectAll(String table){
        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());
        }
        finally{
//          result.close();
//          mydb.close();
        }
        return result;
    }

    public Cursor select(String table,String where){
        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());
        }
        finally{
//          result.close();
//          mydb.close();
        }
        return result;
    }

    public long delete(String table,String condition){
        mydb =this.getWritableDatabase();
        long result = -1;
        try {
            result = mydb.delete(table, condition, null);
        } catch (Exception e) {
            xLog.error(e.getMessage());
        }
        finally{
//          mydb.close();
        }
        return result;
    }

    protected long empty(String table){
        mydb =this.getWritableDatabase();
        long result = -1;
        try {
            result = mydb.delete(table, "", null);
        } catch (Exception e) {
            xLog.error(e.getMessage());
        }
        finally{
//          mydb.close();
        }
        return result;
    }

    public long update(String table,ContentValues cv,String condition){
        mydb =this.getWritableDatabase();
        long result = -1;
        try {
            result = mydb.update(table, cv, condition, null);
        } catch (Exception e) {
            xLog.error(e.getMessage());
        }
        finally{
//          mydb.close();
        }
        return result;
    }

    protected void drop(String table){
        //TODO Produces a damn error!
        mydb =this.getWritableDatabase();
        try {
            mydb.execSQL("DROP TABLE IF EXISTS "+table);
        } catch (Exception e) {
            xLog.error(e.getMessage());
        }
        finally{

        }
    }

}

这是我的日志猫:

11-28 08:04:31.663: E/Database(9025): close() was never explicitly called on database '/data/data/com.example.shareholders/databases/shareholders.db' 
11-28 08:04:31.663: E/Database(9025): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
11-28 08:04:31.663: E/Database(9025):   at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1810)
11-28 08:04:31.663: E/Database(9025):   at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:817)
11-28 08:04:31.663: E/Database(9025):   at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:851)
11-28 08:04:31.663: E/Database(9025):   at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:844)
11-28 08:04:31.663: E/Database(9025):   at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:540)
11-28 08:04:31.663: E/Database(9025):   at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
11-28 08:04:31.663: E/Database(9025):   at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
11-28 08:04:31.663: E/Database(9025):   at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:158)
11-28 08:04:31.663: E/Database(9025):   at ClassLibrary.DbHelper.select(DbHelper.java:141)
11-28 08:04:31.663: E/Database(9025):   at com.example.shareholders.entities.Settings.select(Settings.java:97)
11-28 08:04:31.663: E/Database(9025):   at com.example.shareholders.entities.Settings.getValue(Settings.java:58)
11-28 08:04:31.663: E/Database(9025):   at com.example.shareholders.Login.onCreate(Login.java:32)
11-28 08:04:31.663: E/Database(9025):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-28 08:04:31.663: E/Database(9025):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-28 08:04:31.663: E/Database(9025):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-28 08:04:31.663: E/Database(9025):   at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-28 08:04:31.663: E/Database(9025):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-28 08:04:31.663: E/Database(9025):   at android.os.Handler.dispatchMessage(Handler.java:99)
11-28 08:04:31.663: E/Database(9025):   at android.os.Looper.loop(Looper.java:123)
11-28 08:04:31.663: E/Database(9025):   at android.app.ActivityThread.main(ActivityThread.java:4627)
11-28 08:04:31.663: E/Database(9025):   at java.lang.reflect.Method.invokeNative(Native Method)
11-28 08:04:31.663: E/Database(9025):   at java.lang.reflect.Method.invoke(Method.java:521)
11-28 08:04:31.663: E/Database(9025):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-28 08:04:31.663: E/Database(9025):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-28 08:04:31.663: E/Database(9025):   at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

当您的数据库使用结束时,更好的方法是使用SQLiteDatabase.close()。关闭光标cursor.close();

请记住,如果你不关闭数据库,那么它可能会产生严重的问题