copy database创建数据库但不复制android中db文件中的所有表

时间:2016-09-19 01:48:03

标签: android

我正在尝试从/ src / main / asset中保存的db文件中复制数据库。 Same创建数据库但不创建任何表。有时它会创建android_metadata表,并在其中输入en_us值。但其他表永远不会被复制。以下是我的代码

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

                }

1 个答案:

答案 0 :(得分:0)

我在sqlite浏览器中再次复制了数据库并使用.sqlite生成了该文件。我将此文件放在assets文件夹中,并将数据库名称的名称更改为menu_db.sqlite。有效。我不确定这是否是正确的答案,但我在这里看到了很多这样的问题。对于那些它可能有用。