如何创建一个提供连接而没有任何数据库锁定异常的数据库对象

时间:2014-03-01 09:58:20

标签: android android-sqlite

在我的应用程序中有很多数据库交互,所以我创建了一个包含数据库创建和连接的数据库类以及访问此数据库类进行连接的多个类。

现在我的问题是当这些类访问数据库类时我得到Database Locked异常,Google搜索了很多,有些人建议创建singleton类,但我无法创建这样的类。

我的代码:

主要活动

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Database d=new Database(this);
        Abc a=new Abc(getApplicationContext()) ;
        d.insert();
      //  d.retrive();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

数据库类:

public class Database extends SQLiteOpenHelper{

    private String Tablename = "Table1";
    private String Column1 = "RegionID";
    private String Column2 = "RegionName";
    private String Column3 = "Currency";
    SQLiteDatabase db;

    public Database(Context context) {
        super(context, "Test", null, 2);
        db=this.getWritableDatabase();
        //  db=this.getWritableDatabase();




        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db1) {
        // TODO Auto-generated method stub


        final String r_Table = "CREATE TABLE " + Tablename + " (" + Column1+ " INTEGER PRIMARY KEY , " + Column2 + " TEXT, " + Column3 + " Text) ";
        db.execSQL(r_Table);





    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }
     public void insert(){
         try{
                ContentValues cv=new ContentValues();
                for(int i=1;i<=10;i++){
                cv.put(Column1, i);
                cv.put(Column2, "USA");
                cv.put(Column3, "Dollar");
                db.insert(Tablename, null, cv);
                }
                }
                catch(Exception e){

                    System.out.println(e.getMessage());
                }

    }
     public void retrive(){
         try{
         Cursor c=db.rawQuery("Select * from Table1", null);

         if (c != null ) {
             if  (c.moveToFirst()) {
                 //Toast.makeText(new MainActivity().getApplicationContext(), c.getCount(), Toast.LENGTH_LONG).show();
                 System.out.println("Rows are:"+c.getCount());

         }


     }
         c.close();
         }
         catch(Exception e){

             System.out.println(e.getMessage());
         }
     }

}

访问数据库的另一个类:

import android.content.Context;

public class Abc {

    Context c1;
    public Abc(Context mcontext) {
        // TODO Auto-generated constructor stub
        c1=mcontext;
    }
    Database d=new Database(c1);

}

请帮帮我,告诉我一个出路。

编辑*的 * ** * ** * ** * ** * ** * ** * ** * ** * ** * *** *更改了代码 数据库类:

public class Database extends SQLiteOpenHelper{

    private String Tablename = "Table1";
    private String Column1 = "RegionID";
    private String Column2 = "RegionName";
    private String Column3 = "Currency";
    SQLiteDatabase db;
    private static Database sInstance;
    public Database(Context context) {
        super(context, "Test", null, 2);
        //db=this.getWritableDatabase();
        //  db=this.getWritableDatabase();




        // TODO Auto-generated constructor stub
    }

    public static Database getInstance(Context context) {
        if (sInstance == null) {
            sInstance = new Database(context.getApplicationContext());
        }
        return sInstance;
    }
    @Override
    public void onCreate(SQLiteDatabase db1) {
        // TODO Auto-generated method stub


        final String r_Table = "CREATE TABLE " + Tablename + " (" + Column1+ " INTEGER PRIMARY KEY , " + Column2 + " TEXT, " + Column3 + " Text) ";
        db1.execSQL(r_Table);





    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }
     public void insert(){
         db=getWritableDatabase();
         try{
                ContentValues cv=new ContentValues();
                for(int i=1;i<=10;i++){
                cv.put(Column1, i);
                cv.put(Column2, "USA");
                cv.put(Column3, "Dollar");
                db.insert(Tablename, null, cv);
                }
                }
                catch(Exception e){

                    System.out.println(e.getMessage());
                }

    }
     public void retrive(){
         try{
         Cursor c=db.rawQuery("Select * from Table1", null);

         if (c != null ) {
             if  (c.moveToFirst()) {
                 //Toast.makeText(new MainActivity().getApplicationContext(), c.getCount(), Toast.LENGTH_LONG).show();
                 System.out.println("Rows are:"+c.getCount());

         }


     }
         c.close();
         }
         catch(Exception e){

             System.out.println(e.getMessage());
         }
     }

}

主要活动类

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       // Database d=new Database(this);
       // Database.getInstance(getApplicationContext());
        Abc a=new Abc(getApplicationContext()) ;
        Database.getInstance(getApplicationContext()).insert();
        Database.getInstance(getApplicationContext()).retrive();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

Abc课程

import android.content.Context;

public class Abc {

    Context c1;
    public Abc(Context mcontext) {
        // TODO Auto-generated constructor stub
        c1=mcontext;
        Database.getInstance(c1);
    }


}

1 个答案:

答案 0 :(得分:2)

这就是你需要使用DB(SQLiteOpenHelper的单例):

public class Database extends SQLiteOpenHelper {

    private static Database sInstance;
    private String Tablename = "Table1";
    private String Column1 = "RegionID";
    private String Column2 = "RegionName";
    private String Column3 = "Currency";

    private Database(Context context) {
        super(context, "Test", null, 2);
    }

    public static Database getInstance(Context context) {
        if (sInstance == null) {
            sInstance = new Database(context.getApplicationContext());
        }
        return sInstance;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        String r_Table = "CREATE TABLE " + Tablename + " (" + Column1 + " INTEGER PRIMARY KEY , " + Column2 + " TEXT, " + Column3 + " Text) ";
        db.execSQL(r_Table);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }

    public void insert() {
        SQLiteDatabase db = getWritableDatabase();
        try {
            ContentValues cv = new ContentValues();
            for (int i = 1; i <= 10; i++) {
                cv.put(Column1, i);
                cv.put(Column2, "USA");
                cv.put(Column3, "Dollar");
                db.insert(Tablename, null, cv);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }

    public void retrive() {
        SQLiteDatabase db = getWritableDatabase();
        try {
            Cursor c = db.rawQuery("Select * from Table1", null);
            if (c.moveToFirst()) {
                //Toast.makeText(new MainActivity().getApplicationContext(), c.getCount(), Toast.LENGTH_LONG).show();
                System.out.println("Rows are:" + c.getCount());

            }
            c.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public Cursor retriveCursor() {
        SQLiteDatabase db = getWritableDatabase();

        return db.rawQuery("Select * from Table1", null);

    }

}

因此,您不需要Abc类,只需在帮助程序中实现所有必需的方法即可。要获取实例,只需致电Database.getInstance(context)

编辑如何使用活动:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Database d = Database.getInstance(this);
        d.insert();
        d.retrive();
        Cursor cursor = d.retrieveCursor();
        //do stuff with cursor
        cursor.close;
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}