0

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.

Ali Allahyar
  • 341
  • 2
  • 11
  • 22
  • 2
    In addition to code, post the exception stacktrace from logcat. – laalto Jan 01 '14 at 13:04
  • 1
    @Ali How are you debugging your app using device ? Are you exporting .apk file and installing it on device ? If so try connecting your device to your computer and debug as you do on an emulator. That way you can access log cat and crash logs. BTW with this limited information, its not possible for anyone to help you out now, perhaps if you could post the error logs and source code we can help in debugging. – user88975 Jan 01 '14 at 13:04
  • @user88975 Yes I install the generated .apk file on my device. But I can't try the Debugging mode. My mobile Debugging mode doesn't work on my computer and I can't access it through DDMS. Any idea to fix it? – Ali Allahyar Jan 01 '14 at 13:12
  • Check out this [answer](http://stackoverflow.com/a/3378791/913571) hope it helps – user88975 Jan 01 '14 at 13:15
  • @ laalto, Sorry my mobile debugging mode doesn't work on my computer. I don't know how to fix it. – Ali Allahyar Jan 01 '14 at 13:30
  • @Vigbyor yeah, so I've posted some code... – Ali Allahyar Jan 01 '14 at 13:35
  • 1
    "my mobile debugging mode doesn't work on my computer" -- then you need to fix that first. – CommonsWare Jan 01 '14 at 14:05
  • I suggest for you to uninstall the app first on your phone then reinstall the APK file just to be sure that the database will be recreated. Other than that the logs are the best way to trace where the problem comes from so you really need to get the logs. – KaHeL Jan 01 '14 at 14:05
  • @CommonsWare Dose rooting my device help to fix debugging mode? – Ali Allahyar Jan 01 '14 at 14:10
  • "Dose rooting my device help to fix debugging mode?" -- probably not. However, since you have never asked a question on StackOverflow describing your problem, I have now way to advise you on how to fix it. – CommonsWare Jan 01 '14 at 14:13
  • @KaHeL how did you know that? Well after I cleared data and reinstalled the app it worked. Cool. Thanks a lot for your tip. – Ali Allahyar Jan 01 '14 at 14:13
  • Okay so I'll put that as an answer haahaha! also will tell you on how to get your debugging mode on the answer I will provide. :) – KaHeL Jan 01 '14 at 14:15

1 Answers1

1

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.

KaHeL
  • 4,301
  • 16
  • 54
  • 78