SQLite外部错误Android

时间:2015-11-06 10:32:56

标签: android sqlite

如果退出,我将创建带有数据库的应用程序到外部存储。 没有创建我的数据库。 我的班级DB_Open_helper

package ir.icdc.icdc;

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

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
import android.util.Log;

public class Icdc_DBOpenHelper extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 3;
    private static String DB_PATH = "";
    private static String DB_NAME = "icdc.db";
    private SQLiteDatabase mDataBase;
    private final Context mContext;

    public Icdc_DBOpenHelper(Context context) {
        super(context, DB_NAME, null, DATABASE_VERSION);
        if (check_External_Storage()) {
            DB_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/databases_icdc/";
        } else {
            if (android.os.Build.VERSION.SDK_INT >= 17) {
                DB_PATH = context.getApplicationInfo().dataDir + "/databases_icdc/";
            } else {
                DB_PATH = "/data/data/" + context.getPackageName() + "/databases_icdc/";
            }
        }
        Log.d("PATH" , DB_PATH  + "------------------------" + DB_NAME);
        this.mContext = context;
    }

    public void createDataBase() throws IOException {

        boolean mDataBaseExist = checkDataBase();
        if (!mDataBaseExist) {
            this.getReadableDatabase();
            this.close();
            try {
                copyDataBase();
            } catch (IOException mIOException) {
                throw new Error("ErrorCopyingDataBase");
            }
        }
    }

    private boolean check_External_Storage() {
        String state = Environment.getExternalStorageState();
        if (state.equals(Environment.MEDIA_MOUNTED)) {
            return true;
        } else {
            return false;
        }
    }

    private boolean checkDataBase() {
        File dbFile = new File(DB_PATH + DB_NAME);
        return dbFile.exists();
    }

    private void copyDataBase() throws IOException {
        InputStream mInput = mContext.getAssets().open(DB_NAME);
        String outFileName = DB_PATH + DB_NAME;
        OutputStream mOutput = new FileOutputStream(outFileName);
        byte[] mBuffer = new byte[1024];
        int mLength;
        while ((mLength = mInput.read(mBuffer)) > 0) {
            mOutput.write(mBuffer, 0, mLength);
        }
        mOutput.flush();
        mOutput.close();
        mInput.close();
    }

    public SQLiteDatabase openDataBase() throws SQLException {
        String mPath = DB_PATH + DB_NAME;
        mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.CREATE_IF_NECESSARY);
        return mDataBase;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int ov, int nv) {

    }

    @Override
    public synchronized void close() {
        if (mDataBase != null)
            mDataBase.close();
        super.close();
    }
}

班级主要:

Icdc_DBOpenHelper helper = new Icdc_DBOpenHelper(Sign_up.this);
        try {
            helper.createDataBase();
        } catch (IOException e) {
            e.printStackTrace();
        }

我的错误:

    11-06 13:37:13.472 2460-2460/ir.icdc.icdc E/AndroidRuntime: FATAL EXCEPTION: main 
    11-06 13:37:13.472 2460-2460/ir.icdc.icdc E/AndroidRuntime: Process: ir.icdc.icdc, PID: 2460 
    11-06 13:37:13.472 2460-2460/ir.icdc.icdc E/AndroidRuntime: java.lang.Error: ErrorCopyingDataBase 
    11-06 13:37:13.472 2460-2460/ir.icdc.icdc E/AndroidRuntime:     at ir.icdc.icdc.Icdc_DBOpenHelper.createDataBase(Icdc_DBOpenHelper.java:47) 
    11-06 13:37:13.472 2460-2460/ir.icdc.icdc E/AndroidRuntime:     at ir.icdc.icdc.Sign_up.init(Sign_up.java:56) 
    11-06 13:37:13.472 2460-2460/ir.icdc.icdc E/AndroidRuntime:     at ir.icdc.icdc.Sign_up.onCreate(Sign_up.java:43) 
    11-06 13:37:13.472 2460-2460/ir.icdc.icdc E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5990) 
    11-06 13:37:13.472 2460-2460/ir.icdc.icdc E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)           
    11-06 13:37:13.472 2460-2460/ir.icdc.icdc E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)  
    11-06 13:37:13.472 2460-2460/ir.icdc.icdc E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
    11-06 13:37:13.472 2460-2460/ir.icdc.icdc E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:151) 
    11-06 13:37:13.472 2460-2460/ir.icdc.icdc E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
    11-06 13:37:13.472 2460-2460/ir.icdc.icdc E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
    11-06 13:37:13.472 2460-2460/ir.icdc.icdc E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135) 
    11-06 13:37:13.472 2460-2460/ir.icdc.icdc E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5254) 
    11-06 13:37:13.472 2460-2460/ir.icdc.icdc E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
    11-06 13:37:13.472 2460-2460/ir.icdc.icdc E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 
    11-06 13:37:13.472 2460-2460/ir.icdc.icdc E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)  
    11-06 13:37:13.472 2460-2460/ir.icdc.icdc E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

0 个答案:

没有答案