Autore Topic: Non riesco ad aprire il db in assets  (Letto 512 volte)

Offline Aryon Solutions S.r.l. - Paolo

  • Nuovo arrivato
  • *
  • Post: 42
  • Respect: +1
    • Mostra profilo
  • Dispositivo Android:
    Samsung next
  • Sistema operativo:
    Windows 7
Non riesco ad aprire il db in assets
« il: 21 Settembre 2012, 19:21:01 CEST »
0


09-21 19:15:30.129: E/Database(10311): sqlite3_open_v2("SidebarLoconsole/assets/ShoppingDatabase.sqlite", &handle, 1, NULL) failed


09-21 19:15:55.419: E/AndroidRuntime(10311): FATAL EXCEPTION: main
09-21 19:15:55.419: E/AndroidRuntime(10311): java.lang.NullPointerException
09-21 19:15:55.419: E/AndroidRuntime(10311):    at it.loconsole.sidebar.database.pl_ShoppingDatabase.insertOnUtenteLocal(pl_ShoppingDatabase.java:228)
09-21 19:15:55.419: E/AndroidRuntime(10311):    at it.loconsole.sidebar.demo.pl_Login.Accesso(pl_Login.java:224)
09-21 19:15:55.419: E/AndroidRuntime(10311):    at it.loconsole.sidebar.demo.pl_Login$1.onTouch(pl_Login.java:142)
09-21 19:15:55.419: E/AndroidRuntime(10311):    at android.view.View.dispatchTouchEvent(View.java:3881)
09-21 19:15:55.419: E/AndroidRuntime(10311):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
09-21 19:15:55.419: E/AndroidRuntime(10311):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
09-21 19:15:55.419: E/AndroidRuntime(10311):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
09-21 19:15:55.419: E/AndroidRuntime(10311):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
09-21 19:15:55.419: E/AndroidRuntime(10311):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
09-21 19:15:55.419: E/AndroidRuntime(10311):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1750)
09-21 19:15:55.419: E/AndroidRuntime(10311):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1135)
09-21 19:15:55.419: E/AndroidRuntime(10311):    at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
09-21 19:15:55.419: E/AndroidRuntime(10311):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1734)
09-21 19:15:55.419: E/AndroidRuntime(10311):    at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2216)
09-21 19:15:55.419: E/AndroidRuntime(10311):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1887)
09-21 19:15:55.419: E/AndroidRuntime(10311):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-21 19:15:55.419: E/AndroidRuntime(10311):    at android.os.Looper.loop(Looper.java:130)
09-21 19:15:55.419: E/AndroidRuntime(10311):    at android.app.ActivityThread.main(ActivityThread.java:3687)
09-21 19:15:55.419: E/AndroidRuntime(10311):    at java.lang.reflect.Method.invokeNative(Native Method)
09-21 19:15:55.419: E/AndroidRuntime(10311):    at java.lang.reflect.Method.invoke(Method.java:507)
09-21 19:15:55.419: E/AndroidRuntime(10311):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
09-21 19:15:55.419: E/AndroidRuntime(10311):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
09-21 19:15:55.419: E/AndroidRuntime(10311):    at dalvik.system.NativeStart.main(Native Method)



Vi scrivo anche la classe:
Codice (Java): [Seleziona]
package it.loconsole.sidebar.database;

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

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

public class pl_ShoppingDatabase extends SQLiteOpenHelper{


        private static String DB_PATH = "/data/data/it.loconsole.sidebar.demo/databases/";
         
    private static String DB_NAME = "ShoppingDatabase.sqlite";
 
    private SQLiteDatabase myDataBase;
 
    private final Context myContext;
   
    public pl_ShoppingDatabase(Context context) {
         
        super(context, DB_NAME, null, 1);
        DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
        this.myContext = context;
    }  
   
    public void createDataBase() throws IOException{
         
        boolean dbExist = checkDataBase();
 
        if(dbExist){
                //do nothing - database already exist
        }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();
 
                } catch (IOException e) {
 
                        throw new Error("Error copying database");
 
                }
        }
 
    }
   
    private boolean checkDataBase(){
         
        SQLiteDatabase checkDB = null;
 
        try{
                String myPath = DB_PATH + DB_NAME;
                checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
                Log.i("asdasd", "dasdfasdfasdgf12312ervsdv12341234vfvgf");
        }catch(SQLiteException e){
 
                //database does't exist yet.
 
        }
 
        if(checkDB != null){
 
                checkDB.close();
 
        }
 
        return checkDB != null ? true : false;
    }
   
    private void copyDataBase() throws IOException{
         
        //Open your local db as the input stream
        InputStream myInput = myContext.getAssets().open(DB_NAME);
        Log.i("asdasd", "dasdfasdfasdgfasdasdasdasdasd");
        // Path to the just created empty db
        String outFileName = DB_PATH + DB_NAME;
        Log.i("asdasd", "dasdfasdfasdgfeqwe2342342344");
        //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();
 
    }
 
    public void openDataBase() throws SQLException{
 
        //Open the database
        String myPath = DB_PATH + DB_NAME;
        Log.i("asdasd", "dasdfasdfasdgf");
        myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
 
    }
   
    @Override
        public synchronized void close() {
 
            if(myDataBase != null)
                    myDataBase.close();
 
            super.close();
 
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
               
//              try {
//                      createDataBase();
//                      Log.i("pl_ShoppingDatabase", "Database creato!");
//              } catch (IOException e) {
//                      // TODO Auto-generated catch block
//                      Log.i("pl_ShoppingDatabase", "Database non creato!");
//                      e.printStackTrace();
//              }
               
        }

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