lundi 6 juillet 2015

Error trying to read from an sqlite file (Logcat : SQLiteException)

I've created an sqlite file , and placed in the projects directory. It's still empty but I ran my program to see if it even works - and it doesn't (I've written a certain Log as an indication). Here's the code of the SQLiteOpenHelper subclass . Thank you very much.

import java.util.Map;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

class DatabaseLoader extends SQLiteOpenHelper
{
    private static DatabaseLoader instance;
    private Map<String,Password> passwords ;
    private static final String ERROR_OPEN_FILE_TAG = "Error Opening File";
    private static final String DATABASE_NAME = "passwords.sql";
    private static final int DATABASE_VERSION = 1;

    public static synchronized DatabaseLoader getInstance( Context context ) 
    {
        if (instance == null) 
        {
          instance = new DatabaseLoader( context.getApplicationContext() );
        }
        return instance;
    }

    private DatabaseLoader( Context context ) {
        super( context, DATABASE_NAME, null, DATABASE_VERSION );
    }

    public Map<String,Password> loadDatabase()
    {
        try
        {
            SQLiteDatabase db = this.getWritableDatabase();
            Cursor result =  db.rawQuery( "select * from passwords", null );
            result.moveToFirst();

            while( !result.isAfterLast() )
            {
                String password = result.getString( 1 );
                int date = result.getInt( 2 );
                passwords.put( password , new Password( password, date) );
                result.moveToNext();
            }
        }
        catch( SQLiteException errorOpeningDB )
        {
            Log.d( ERROR_OPEN_FILE_TAG, "SQLiteException - error opening db for writing" );
        }

        return passwords;
    }



    @Override
    public void onCreate( SQLiteDatabase db )
    {
        // TODO Auto-generated method stub

    }

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

    }
}

Aucun commentaire:

Enregistrer un commentaire