2015-11-11 30 views
-1

我想複製SQLite數據庫。
複製數據庫的代碼是:無法從SQLite數據庫進行查詢:沒有這樣的表錯誤

public class DBHelper extends SQLiteOpenHelper { 

    private static final String DB_PATH="/data/data/com.nirmalam.vaishnavismeclass.mydatabaseapplication/databases/"; 
    private static final String DB_NAME="vaishnavismeclass.db"; 
    private static final int SCHEMA_VERSION=1; 

    public SQLiteDatabase dbSQLite; 

    private final Context myContext; 

    public DBHelper(Context context){ 
     super(context, DB_NAME, null, SCHEMA_VERSION); 
     this.myContext = context; 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 

    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     if(newVersion>oldVersion) 
      copyDBFromResource(); 
    } 

    public void createDatabase(){ 
     createDB(); 
    } 

    private void createDB() { 
     boolean dbExist = DBExist(); 

     if (!dbExist){ 
      this.getReadableDatabase(); 

      copyDBFromResource(); 
     } 
    } 

    private void copyDBFromResource() { 

     InputStream inputStream = null; 
     OutputStream outputStream = null; 
     String dbFilePath = DB_PATH + DB_NAME; 

     try{ 
      inputStream = myContext.getAssets().open(DB_NAME); 
      outputStream = new FileOutputStream(dbFilePath); 

      byte[] buffer = new byte[1024]; 
      int length; 
      while ((length = inputStream.read(buffer))>0){ 
       outputStream.write(buffer,0,length); 
      } 
      outputStream.flush(); 
      outputStream.close(); 
      inputStream.close(); 

     } 
     catch (IOException e) { 

      e.printStackTrace(); 
      throw new Error("Problem copying data to storage"); 
     } 
    } 
    private boolean DBExist(){ 
     SQLiteDatabase db=null; 

     try { 
      String databasePath = DB_PATH + DB_NAME; 
      db = SQLiteDatabase.openOrCreateDatabase(databasePath, null); 
      db.setLocale(Locale.getDefault()); 
      db.setVersion(SCHEMA_VERSION); 
     } 
     catch (SQLiteException e){ 
      Log.e("VC","SQLite Database not found"); 
     } 

     if (db != null){ 
      db.close(); 
     } 

     return db !=null ?true :false; 
    } 
    @Override 
    public synchronized void close(){ 
     if (dbSQLite != null){ 
      dbSQLite.close(); 
     } 
     super.close(); 

    } 
} 

使用Android設備監視器,我可以看到數據庫下得了/data/data/<packagename>/databases

創建當我嘗試使用此代碼

進行查詢檢索數據
public Cursor getSinglePasuram(int id){ 

     Cursor mCursor; 
     String databasePath = DB_PATH + DB_NAME; 
     // SQLiteDatabase db = this.getReadableDatabase(); 
     SQLiteDatabase db = SQLiteDatabase.openDatabase(databasePath, null,SQLiteDatabase.OPEN_READONLY); 
     Log.d("VC","db is "+db.getPath()+db.getVersion()); 
     mCursor = db.rawQuery("select * from pasurams where _id = 1", null); 

     return mCursor; 

    } 

找不到表pasurams的運行時錯誤。相同的查詢在SQLite數據庫中獨立運行良好。

這段代碼有什麼問題?任何幫助表示讚賞。

+0

從設備檢索數據庫文件進行檢查,沒有表。意思是複製不正確。 –

回答

0

應用程序使用的數據庫與您在SQLiteManager中查詢的數據庫不同。
您可能在第一次運行後添加了表格。

當現在運行時,它已經找到了分貝並不會複製一個從assets文件夾。 此行if (!dbExist){(正確)可防止將新的 db複製到舊的之一。

您必須從您的databases路徑中刪除數據庫並重新運行該應用程序。

[編輯]

這是我如何複製分貝

// Open your local db as the input stream 
final InputStream is = ctx.getAssets().open(DB_NAME); 

// Path to the just created empty db 
final OutputStream os = new FileOutputStream(new File(DB_PATH)); 

// Transfer the bytes from the inputfile to the outputfile 
final byte[] bfr = new byte[8192];//[1024]; 

int len = 0; 

while ((len = is.read(bfr)) > 0) 
{ 
    os.write(bfr, 0, len); 
} 

// Close the streams 
os.flush(); 
os.close(); 
is.close(); 

注:CTX是我通過上下文。

+0

試圖將數據庫直接複製到設備,它工作正常。如果我刪除數據庫,它複製空白數據庫。在copyDBFromResource()中懷疑問題 –

相關問題