2016-09-19 11 views
0

/src/main/assetに保存されたdbファイルからデータベースをコピーしようとしています。同じデータベースを作成しますが、テーブルを作成しません。場合によっては、android_metadataテーブルを作成し、en_us値を入力することもあります。しかし、他のテーブルは決してコピーされません。以下は私のコードですコピーデータベースはデータベースを作成しますが、アンドロイドのdbファイルからすべてのテーブルをコピーしません

09-19 12:17:53.493 32261-32313/com.example.restcalc W/System: ClassLoader referenced unknown path: /data/data/com.example.restcalc/lib 
09-19 12:17:54.169 32261-32261/com.example.restcalc I/Choreographer: Skipped 46 frames! The application may be doing too much work on its main thread. 
09-19 12:18:03.683 32261-32271/com.example.restcalc I/art: Background sticky concurrent mark sweep GC freed 2831(113KB) AllocSpace objects, 0(0B) LOS objects, 9% free, 2MB/2MB, paused 35.551ms total 58.953ms 
09-19 12:18:03.721 32261-32271/com.example.restcalc W/art: Suspending all threads took: 38.121ms 
09-19 12:18:07.035 32261-32261/com.example.restcalc W/System.err: java.io.FileNotFoundException: menu_db 
09-19 12:18:07.035 32261-32261/com.example.restcalc W/System.err:  at android.content.res.AssetManager.openAsset(Native Method) 
09-19 12:18:07.035 32261-32261/com.example.restcalc W/System.err:  at android.content.res.AssetManager.open(AssetManager.java:313) 
09-19 12:18:07.035 32261-32261/com.example.restcalc W/System.err:  at android.content.res.AssetManager.open(AssetManager.java:287) 
09-19 12:18:07.035 32261-32261/com.example.restcalc W/System.err:  at com.example.restcalc.DbOperations$override.copyDatabase(DbOperations.java:36) 
09-19 12:18:07.036 32261-32261/com.example.restcalc W/System.err:  at com.example.restcalc.DbOperations$override.access$dispatch(DbOperations.java) 
09-19 12:18:07.036 32261-32261/com.example.restcalc W/System.err:  at com.example.restcalc.DbOperations.copyDatabase(DbOperations.java:0) 
09-19 12:18:07.036 32261-32261/com.example.restcalc W/System.err:  at com.example.restcalc.MainPage$2.onClick(MainPage.java:153) 
09-19 12:18:07.036 32261-32261/com.example.restcalc W/System.err:  at android.view.View.performClick(View.java:5198) 
09-19 12:18:07.036 32261-32261/com.example.restcalc W/System.err:  at android.view.View$PerformClick.run(View.java:21147) 
09-19 12:18:07.036 32261-32261/com.example.restcalc W/System.err:  at android.os.Handler.handleCallback(Handler.java:739) 
09-19 12:18:07.036 32261-32261/com.example.restcalc W/System.err:  at android.os.Handler.dispatchMessage(Handler.java:95) 
09-19 12:18:07.036 32261-32261/com.example.restcalc W/System.err:  at android.os.Looper.loop(Looper.java:148) 
09-19 12:18:07.036 32261-32261/com.example.restcalc W/System.err:  at android.app.ActivityThread.main(ActivityThread.java:5417) 
09-19 12:18:07.036 32261-32261/com.example.restcalc W/System.err:  at java.lang.reflect.Method.invoke(Native Method) 
09-19 12:18:07.036 32261-32261/com.example.restcalc W/System.err:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
09-19 12:18:07.036 32261-32261/com.example.restcalc W/System.err:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
09-19 12:18:08.038 32261-32261/com.example.restcalc I/Choreographer: Skipped 59 frames! The application may be doing too much work on its main thread. 
09-19 12:18:08.507 32261-32267/com.example.restcalc W/art: Suspending all threads took: 17.206ms 
09-19 12:18:45.535 32261-32267/com.example.restcalc W/art: Suspending all threads took: 6.309ms 
09-19 12:21:30.767 32261-32267/com.example.restcalc W/art: Suspending all threads took: 6.321ms 
09-19 12:21:46.789 32261-32267/com.example.restcalc W/art: Suspending all threads took: 5.514ms 

                       --------- beginning of system 
09-19 12:22:32.849 32261-32267/com.example.restcalc W/art: Suspending all threads took: 5.377ms 
09-19 12:23:05.924 32261-32267/com.example.restcalc W/art: Suspending all threads took: 9.828ms 
09-19 12:27:40.675 32261-32267/com.example.restcalc W/art: Suspending all threads took: 6.375ms 
09-19 12:30:18.409 32261-32267/com.example.restcalc W/art: Suspending all threads took: 10.667ms 
09-19 12:31:43.045 32261-32267/com.example.restcalc W/art: Suspending all threads took: 11.935ms 


DbOperation.java 
package com.example.restcalc; 

import android.content.Context; 
import android.database.Cursor; 
import android.database.DatabaseErrorHandler; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 
import android.widget.Toast; 

import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 

/** 
* Created by User1 on 24/08/2016. 
*/ 

public class DbOperations extends SQLiteOpenHelper { 

    public static final String DB_PATH = "data/data/com.example.restcalc/databases/"; 
    private static final String LOG = ""; 
    private Context context; 
    private SQLiteDatabase mDatabase; 

    public DbOperations(Context context) { 
     super(context, DbData.DATABASE_NAME, null, 1); 
     this.context = context; 
    } 

    public boolean copyDatabase (Context context) { 

     try { 
      // Open your local db as the input stream 
      InputStream inputStream = context.getAssets().open(DbData.DATABASE_NAME); 
      // Path to the just created empty db 
      String outFilename = DB_PATH + DbData.DATABASE_NAME; 

      // Open the empty db as the output stream 
      OutputStream outputStream = new FileOutputStream(outFilename); 

      byte[] buffer = new byte[1024]; 
      int length = 0; 
      while ((length = inputStream.read(buffer)) > 0) { 
       outputStream.write(buffer, 0, length); 
      } 
      outputStream.flush(); 
      outputStream.close(); 
      return true; 
     } catch (IOException e) { 
      e.printStackTrace(); 
      return false; 
     } 
    } 

    @Override 
    public void onCreate(SQLiteDatabase sqLiteDatabase) { 

    } 

    @Override 
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { 

    } 

    public void openDatabase() { 
     String dbPath = context.getDatabasePath(DbData.DATABASE_NAME).getPath(); 
     if (mDatabase != null && mDatabase.isOpen()) { 
      return; 
     } 
     mDatabase = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READONLY); 
    } 

    public void closeDatabase() { 
     if (mDatabase != null) { 
      mDatabase.close(); 
     } 
    } 

    public Cursor retrieveData (DbOperations dbOperations) { 
     SQLiteDatabase sqldb = dbOperations.getReadableDatabase(); 
     String selectQuery = "SELECT item_rate FROM " + DbData.menuItem_table 
       + " WHERE item_number = " + MainPage.item_number; 
     Log.d(LOG,selectQuery); 
     Cursor cursor = sqldb.rawQuery(selectQuery, null); 
     return cursor; 
    } 

} 



MainPage.java 
//calling the DbOperations 

DbOperations dbOperations = new DbOperations(context); 

        //Check database exists 
        File database = getApplicationContext().getDatabasePath(DbData.DATABASE_NAME); 
        if (false == database.exists()) { 
         //copy db 
         if (dbOperations.copyDatabase(getApplicationContext())) { 
          Toast.makeText(context, "Copy Database Success", Toast.LENGTH_LONG).show(); 
         } else { 
          Toast.makeText(context, "copy Database Error", Toast.LENGTH_LONG).show(); 
          return; 
         } 
        } else { 
         Toast.makeText(context, "Database Exists", Toast.LENGTH_LONG).show(); 

         Cursor cursor = null; 

         cursor = dbOperations.retrieveData(dbOperations); 

         if (cursor.moveToFirst()) { 
          itemRate = cursor.getInt(0); 

          int a = itemRate * Integer.valueOf(itemQty.getText().toString()); 

          a = a + 40; // add service charge 

          intent.putExtra("Total", a); 

          startActivity(intent); 
         } else { 
          Toast.makeText(getBaseContext(),"Item Rate Not Found", Toast.LENGTH_LONG).show(); 
         } 
        } 

       } 

答えて

0

私はsqliteブラウザでデータベースを再度コピーし、.sqliteでファイルを生成しました。このファイルをassetsフォルダに置き、データベース名をmenu_db.sqliteに変更しました。出来た。私はこれが正解であるかどうかはわかりませんが、私はここでこのような質問を多く見ました。それらのためには役に立つかもしれません。

関連する問題