2014-03-01 61 views
0

在我的應用程序中有很多數據庫交互,所以我創建了一個數據庫類來存放數據庫的創建和連接以及訪問這個數據庫類的多個類連接。如何創建一個沒有任何數據庫鎖定異常的數據庫對象

現在我的問題在這裏是當這些類訪問數據庫類我得到Database Locked異常,谷歌搜索了很多,有人建議創建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); 
    } 


} 

回答

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; 
    } 

} 
+0

感謝您的回覆@nikis我在這裏有一些疑慮。 1.'SQLiteDatabase db = getWritableDatabase();'如果這行在'constructor'中不存在,那麼控件不會去'onCreate',你的例子也有相同的代碼,那麼控件將如何創建。 2.通過重複調用'getWritableDatabase()'我們不會得到數據庫鎖定異常..也請你解釋一下這個單例類的工作。 – Siva

+0

1.它會去onCreate,因爲你通過將它的名字傳遞給super來在你的db中插入。2不,我們不這樣做,因爲:一旦打開成功,數據庫就會被緩存,所以你可以每次需要寫入數據庫時​​調用這個方法 – nikis

+0

好吧,單例很簡單 - 每次你試圖獲取它的實例,你將使用同一個對象。 – nikis