samedi 12 mars 2016

error for open the sqlite database with android studio

Can i know why when i run this project it was out this error and the app on device was unfortunely,app has stopped.the error is . android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database

package dijkstra.app.com.demodistancev8;

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

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

//http://ift.tt/1pgsWV1

public class SQLHelper extends SQLiteOpenHelper{

private static final String DATABASE_NAME = "schoolnav.sqlite";
private static final int DATABASE_VERSION = 1;
private static String DB_PATH = "/data/data/com.app.dijkstra/databases/";
private Context myContext;

public SQLHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    // TODO Auto-generated constructor stub
    myContext=context;
}

public void createDataBase() throws IOException{
    if(DataBaseisExist()){
        //do nothing - database already exist
        Toast.makeText(myContext, "Database Sudah Ada", Toast.LENGTH_LONG).show();
    }
    else{
        //By calling this method and empty database will be created into the default system path
           //of your application so we are gonna be able to overwrite that database with our database.
        this.getReadableDatabase();

        try {
            copyDataBase();
            Toast.makeText(myContext, "Database Berhasil Diimport Dari Assets", Toast.LENGTH_LONG).show();
        } catch (IOException e) {
            throw new Error("Error copying database");
        }
    }

}

private boolean DataBaseisExist(){
    SQLiteDatabase checkDB = null;
    try{
        String myPath = DB_PATH + DATABASE_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

    }catch(SQLiteException e){
        //database does't exist yet.
    }
    if(checkDB != null){
        checkDB.close();
    }
    if(checkDB != null )return true ;else return false;
}

private void copyDataBase() throws IOException{
    //Open your local db as the input stream
    InputStream myInput = myContext.getAssets().open(DATABASE_NAME);
    // Path to the just created empty db
    String outFileName = DB_PATH + DATABASE_NAME;
    //Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);
    //transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer))>0){
        myOutput.write(buffer, 0, length);
    }
    //Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();
}

@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub

}
}}


<?xml version="1.0" encoding="utf-8"?>

<permission android:name="dijkstra.app.com.demodistancev8.permission.MAPS_RECEIVE"
    android:protectionLevel="signature"></permission>
<uses-permission android:name="dijkstra.app.com.demodistancev8.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission     android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.SEND_SMS" />
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
     Google Maps Android API v2, but are recommended.
-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="@string/google_maps_key" />

    <activity
        android:name=".MapsActivity"
        android:label="@string/title_activity_maps" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

Aucun commentaire:

Enregistrer un commentaire