Question

I've written an app that uses SQLite db. At the first stage when I wasn't using SQLite and just working with ordinary and usual fields to store data, my app was working just fine in my mobile phone. But Now when I'm using SQLite to efficiently store and extract data, my app crashes in my mobile phone. The interesting point is that it is working well on AVD emulator in my computer. Any idea why this is happening?

This is my DB class:

public class WordsDB extends SQLiteOpenHelper {

    static final String DATABASE_NAME = "DicDB";

    static final String TermTable = "Terms";
    static final String ID = "id";
    static final String Selected = "selected";
    static final String Term = "term";
    static final String Br = "br_phon";
    static final String NAm = "nam_phon";
    static final String Type = "type";
    static final String Noun = "noun";
    static final String Verb = "verb";
    static final String Adjective = "adjective";
    static final String Adverb = "adverb";

    static final String DefTable = "Definitions";
    static final String Def = "definition";
    static final String Ex = "example";

    public static final int DATABASE_VERSION = 1;

    private static final String TERM_TABLE_CREATE = "Create table " + TermTable +
        "(" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
        Selected + " VARCHAR(5) ," +
        Term + " VARCHAR(20) ," +
        Br + " VARCHAR(20) ," +
        NAm + " VARCHAR(20) ," +
        Type + " VARCHAR(10) ," +
        Noun + " VARCHAR(20) ," +
        Verb + " VARCHAR(20) ," +
        Adjective + " VARCHAR(20) ," +
        Adverb + " VARCHAR(20)) ";

    private static final String DEF_TABLE_CREATE = "Create table " + DefTable +
        "(" + ID + " INTEGER PRIMARY KEY ," +
        Def + " VARCHAR(30) ," +
        Ex + " VARCHAR(160)) ";

    public WordsDB(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {
    db.execSQL(TERM_TABLE_CREATE);
    db.execSQL(DEF_TABLE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        db.execSQL("DROP TABLE IF EXISTS " + TermTable);
        db.execSQL("DROP TABLE IF EXISTS " + DefTable);
        onCreate(db);
    }
}

And here is the DataSource class:

public class WordsDS {

    private WordsDB dbHelper;
    private SQLiteDatabase db;

    public WordsDS(Context context) {
        dbHelper = new WordsDB(context);
    }

    public void open() throws SQLException {
        db = dbHelper.getWritableDatabase();
    }

    public void close() {
        db.close();
    }

    // Methods to inset, update and extract data from table
    .......
}

I create an object out of WordsDS class in my main activity and through open method I get writable database and so forth and in my other classes I use the WordsDS object and insert, update and extract data through it.

Was it helpful?

Solution

First you must uninstall the app to make a fresh install. Meaning all the database created will be wiped on your phone. This will ensure that if a new column is added or changes in database structure occurs the database will be created. You might think that the onUpgrade method will do this trick for you but it will not!

As for debugging your app on mobile. First you must install the drivers of your phone on your computer. Then you can now run the adb logcat afterwards.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top