mardi 7 juillet 2015

transferring JSON file's data into SQLite

I've written a short program to transfer the data from a JSON file into a SQLite file, and although I'm not getting any errors , it's just isn't working.

My goal is to build an app that will have a default SQLite db file that the app will load on startup. Since I have around 10k records , instead of creating them from the program and doing "insert" 10k times - I created an sqlite file. Then I needed to somehow initialize that db file. So I wrote a short program just for that. It takes a JSON file , parses it and saves on the db file. It seems to me that the ContentValues object is null even though I override its key-value pair multiple times.

the code :

    public class jsonToDb
{
    static void transfer( WeakReference<Context> context )
    {
        DatabaseLoader.init( context );
        String COLUMN_PASSWORD = "password";
        String COLUMN_COUNT = "success_count";

        ContentValues cv = new ContentValues();
        JSONParser parser = new JSONParser();

        try 
        {
            Object obj = parser.parse( new FileReader(
                    "/Users/user/Downloads/convert.json" ) );

            JSONArray jsonArray = (JSONArray) obj;
            for (int i=0 ; i<jsonArray.length() ; i++){
                String password = (String) jsonArray.getJSONObject( i ).get( "Code" );
                Integer countInt =  (Integer) jsonArray.getJSONObject( i ).get( "Count" );
                int count = countInt.intValue();
                cv.put( "password" ,password ); 
                cv.put( "success_count" , count );
                DatabaseLoader.getInstance().transferJson( cv );
            }   
        }    
        catch( Exception e )
        {
            Log.d( "TAG", "not working" );
        }

    }
}

the code in my database helper :

public void transferJson( ContentValues cv )
    {
        String TABLE_PASSWORDS = "passwords";
        try
        {
            getDb().insert( TABLE_PASSWORDS, null, cv );
        }
        catch( SQLiteException errorOpeningDB )
        {
            Log.e( ERROR_OPEN_FILE_TAG, "problem" ,errorOpeningDB );
        }

then I get that "problem" log.

Thank you very much.

Aucun commentaire:

Enregistrer un commentaire