SQLiteCantOpenDatabaseException:未知错误(代码14):数据库不起作用

时间:2015-02-05 21:19:14

标签: java android sqlite

我开发了一个android应用程序项目,它使用带有FrDb类的mysqlite数据库,并且运行良好,使用以下代码:

FrDb  mFrDb = new FrDb (getApplicationContext());
SQLiteDatabase db = mFrDb.getWritableDatabase();

我在另一个项目中复制这些类,并尝试使用相同的方法创建一个新的数据库。 在新项目中,相同的代码不起作用,它不会创建数据库。 在android studio的logcat中我找到了:

SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database

在调试器中,变量savedInstanceState为null。

你知道如何解决我的问题吗? 感谢

这里是主类和Db助手类



package com.android.myapp;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class FrDb extends SQLiteOpenHelper {
    //Nome del database che vogliamo creare
    private static final String DB_NOME = "ForrestDb";

    
    private static final int DB_VERSIONE = 1;

    public FrDb(Context context) {
        super(context, DB_NOME, null, DB_VERSIONE);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
		
        String sql = "CREATE TABLE Allenamenti";
        sql += "(_id INTEGER PRIMARY KEY, nomeAllenamento VARCHAR NOT NULL, dataAllenamento LONG, " +
                " distanzaPercorsa DOUBLE, passoMedioComplessivo DOUBLE, velocitaMedia DOUBLE," +
                " lunghezzaPasso DOUBLE, passoMedioStabilito DOUBLE, ultimiXMetri INTEGER, velocita " +
                " VARCHAR, passo VARCHAR, elevazione VARCHAR);";

        
        db.execSQL(sql);



    }

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

    }


    public void popolaTabella(ClasseAllenamento allenamentoDaSalvare) throws JSONException {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        /*
            Carico tutti i dati come coppie nome-valore
         */
        //Dati di fine allenamento
        String nomeAllenamento = allenamentoDaSalvare.getNomeAllenamento();
        double distanzaPercorsa = allenamentoDaSalvare.getDistanzaPercorsa();
        double passoMedioComplessivo = allenamentoDaSalvare.getPassoMedioComplessivo();
        double velocitaMedia = allenamentoDaSalvare.getVelocitaMedia();
        double lunghezzaPasso = allenamentoDaSalvare.getLunghezzaPasso();
        double passoMedioStabilito = allenamentoDaSalvare.getPassoMedioStabilito();
        int ultimiXMetri = allenamentoDaSalvare.getUltimiXMetri();
        ArrayList<Double> velocita = allenamentoDaSalvare.getVelocita();
        ArrayList<Double> passo = allenamentoDaSalvare.getPasso();
        ArrayList<Integer> elevazione = allenamentoDaSalvare.getElevazione();


        contentValues.put("nomeAllenamento", nomeAllenamento);
        contentValues.put("distanzaPercorsa", distanzaPercorsa);
        contentValues.put("passoMedioComplessivo", passoMedioComplessivo);
        contentValues.put("velocitaMedia", velocitaMedia);
        //dati preimpostati
        contentValues.put("lunghezzaPasso", lunghezzaPasso);
        contentValues.put("passoMedioStabilito", passoMedioStabilito);
        contentValues.put("ultimiXMetri", ultimiXMetri);

        //variabili singoli campionamenti
        //Serializzazione Json per gli ArrayList
        JSONObject json = new JSONObject();
        json.put("velocita", new JSONArray(velocita));
        String velocitaS = json.toString();
        json = new JSONObject();
        json.put("passo", new JSONArray(passo));
        String passoS = json.toString();
        json = new JSONObject();
        json.put("elevazione", new JSONArray(elevazione));
        String elevazioneS = json.toString();

        contentValues.put("velocita", velocitaS);
        contentValues.put("passo", passoS);
        contentValues.put("elevazione", elevazioneS);


        db.insert("Allenamenti", null, contentValues);
        //Toast.makeText(getApplicationContext(), "Nuovo allenamento salvato", Toast.LENGTH_SHORT).show();

    }



    public ArrayList<ClasseAllenamento> getAllenamentoSalvato () throws JSONException {
        SQLiteDatabase db = this.getWritableDatabase();
        //Tutte le colonne della tabella eccetto l'id
        String[] columns = {"_id","nomeAllenamento","dataAllenamento","distanzaPercorsa","passoMedioComplessivo","velocitaMedia","lunghezzaPasso","passoMedioStabilito","ultimiXMetri","velocita","passo","elevazione"};

        //Recupero
        Cursor c = db.query("Allenamenti", columns, null, null, null, null, null, null /*orderBy*/);
        //Creo l'allenamento che verrà restituito dal metodo
        ArrayList <ClasseAllenamento> allenamenti = new ArrayList<ClasseAllenamento>();
        ClasseAllenamento allenamentoSalvato = new ClasseAllenamento();
        c.moveToFirst();
        while(c.moveToNext()) {
            allenamentoSalvato.setNomeAllenamento(c.getString(c.getColumnIndex("nomeAllenamento")));
            allenamentoSalvato.setDataAllenamento(c.getString(c.getColumnIndex("dataAllenamento")));
            allenamentoSalvato.setDistanzaPercorsa(c.getDouble(c.getColumnIndex("distanzaPercorsa")));
            allenamentoSalvato.setPassoMedioComplessivo(c.getDouble(c.getColumnIndex("passoMedioComplessivo")));
            allenamentoSalvato.setVelocitaMedia(c.getDouble(c.getColumnIndex("velocitaMedia")));
            allenamentoSalvato.setLunghezzaPasso(c.getDouble(c.getColumnIndex("lunghezzaPasso")));
            allenamentoSalvato.setPassoMedioStabilito(c.getDouble(c.getColumnIndex("passoMedioStabilito")));
            allenamentoSalvato.setUltimiXMetri((c.getInt(c.getColumnIndex("ultimiXMetri"))));
            allenamentoSalvato.setIdAllenamento((c.getLong(c.getColumnIndex("_id")))); //l'indice dell'allenamento viene salvato nell'oggetto per consentirne l'eliminazione
            String stringaVel = (c.getString(c.getColumnIndex("velocita")));
            String stringaPas = (c.getString(c.getColumnIndex("passo")));
            String stringaEle = (c.getString(c.getColumnIndex("elevazione")));

            //Deserializzo gli ArrayList

            ArrayList <Double> velocita = JsonUtil.jsonToListDouble(stringaVel, "velocita");
            ArrayList <Double> passo = JsonUtil.jsonToListDouble(stringaPas, "passo");
            ArrayList <Integer> elevazione = JsonUtil.jsonToListInt(stringaEle, "elevazione");

            allenamentoSalvato.setVelocita(velocita);
            allenamentoSalvato.setPasso(passo);
            allenamentoSalvato.setElevazione(elevazione);

            allenamenti.add(allenamentoSalvato);


        }

        return allenamenti;

    }

    //elimina un allenamento dal database restituisce true se l'operazione è andata a buon fine
    public boolean cancellaAllenamento(/*SQLiteDatabase db, */long idAllenamento) {
        SQLiteDatabase db = getReadableDatabase();
        return db.delete("allenamenti", "_id" + "=" + idAllenamento, null) > 0;
    }

    //Restituisce il numero di allenamenti presenti nel databse
    public Integer numeroAllenamenti() {
        //Chiediamo l'accesso al db
        SQLiteDatabase db = getReadableDatabase();

        //Query per recuperare il numero di righe presenti nel db
        final String sql = "SELECT COUNT(*) FROM allenamenti";

        Cursor c = db.rawQuery(sql, null);
        int numero = 0;
        if(c.moveToFirst()) {
            numero = c.getInt(0);
        }
        return numero;



    }
    public boolean checkDataBase() {
        SQLiteDatabase checkDB = null;
        try {
            checkDB = SQLiteDatabase.openDatabase(DB_NOME, null,
                    SQLiteDatabase.OPEN_READONLY);
            checkDB.close();
        } catch (SQLiteException e) {
            // database doesn't exist yet.
        }
        return checkDB != null ? true : false;
    }

}
&#13;
&#13;
&#13;

package com.android.myapp;


import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.android.forrestrunner.LocationLoggerService.LocalBinder;
import com.android.forrestrunner.LocationLoggerService.OnNewGPSPointsListener;

import org.json.JSONException;

import java.text.DecimalFormat;


public class Main extends Activity {




    private TextView tvLatitudine;
    private TextView tvLongitudine;
    private TextView tvAltitude;
    private TextView tvTime;
    private TextView tvDistanzam;
    // The primary interface we will be calling on the service
    private LocationLoggerService mService = null;
    private boolean mIsBound;
    // Riferimento all'Intent associato al Service
    private Intent serviceIntent;
    public  String opzione1;
    public  String opzione2;
    public  String opzione3;
    SharedPreferences opzioni;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Inizializziamo l'Intent del servizio
        serviceIntent = new Intent(this, LocationLoggerService.class);

        //Inizializziamo le due TextView
        tvLatitudine = (TextView)this.findViewById(R.id.tvLatitudine);
        tvLongitudine=(TextView)this.findViewById(R.id.tvLongitudine);
        tvAltitude=(TextView)this.findViewById(R.id.tvAltezza);
        tvTime=(TextView)this.findViewById(R.id.tvTime);
        tvDistanzam=(TextView)this.findViewById(R.id.tvDistanzam);

        opzioni = PreferenceManager.getDefaultSharedPreferences(this);
        opzione1 = opzioni.getString(Settings.KEY_OPT1,"");
        opzione2 = opzioni.getString(Settings.KEY_OPT2,"");
        opzione3 = opzioni.getString(Settings.KEY_OPT3,"");
         FrDb mFrDb = null;

        mFrDb = new FrDb(getApplicationContext());
        SQLiteDatabase db = mFrDb.getWritableDatabase();
        /*Boolean bool = mFrDb.checkDataBase();
}


    //Prova delle settings
    @Override
    protected void onStart() {
    super.onStart();

        TextView t1 = (TextView)
                this.findViewById(R.id.Opzione1);
        TextView t2 = (TextView)
                this.findViewById(R.id.Opzione2);
        TextView t3 = (TextView)
                this.findViewById(R.id.Opzione3);

        t1.setText("Ultimi X metri = "+ opzione1);
        t2.setText("Lunghezza passo = "+ opzione2);
        t3.setText("Passo medio = "+ opzione3);
    }

    ////_____________________________________________________________________________________________________
    //inizio parte di collegamento e scollegamento al service

    //al tocco del pulsante START
    public void startGPS(View view) {

        Toast.makeText(this, "Corri!",
                Toast.LENGTH_SHORT).show();
        connectLocalService();

    }

    //al tocco del pulsante PAUSA
    public void pausaGPS(View view) throws JSONException /*throws JSONException*/ {

        Toast.makeText(this, "Pausa.", Toast.LENGTH_SHORT).show();




        //Deregistro il listener
        mService.removeOnNewGPSPointsListener();
        // si disconnette (unBind)
        disconnectLocalService();
    }


    /**
     * Class for interacting with the main interface of the service.
     */
    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName className, IBinder
                service) {
            // This is called when the connection with the service has been
            // established, giving us the service object we can use to
            // interact with the service.
            LocalBinder binder = (LocalBinder) service;
            mService = binder.getService();
            mIsBound = true;








            OnNewGPSPointsListener clientListener = new LocationLoggerService.OnNewGPSPointsListener() {
                @Override
                public void onNewGPSPoint() {
                    getGPSData();
                }
            };






            //Registriamo il listener per ricevere gli aggiornamenti
            mService.addOnNewGPSPointsListener(clientListener);
        }
        @Override
        public void onServiceDisconnected(ComponentName className) {
            // This is called when the connection with the service has been
            // unexpectedly disconnected -- that is, its process crashed.
            mIsBound = false;
            // qui andra' gestita la situazione...
        }
    };



//_____________________________________________________________________________________________________


}

0 个答案:

没有答案