-1

This is my current code for creating the table:

@Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        String CREATE_USER_TABLE = "CREATE TABLE " + TABLE_USERS + "(" + KEY_USERID + " INTEGER PRIMARY KEY,"   
        + KEY_USERNAME + " TEXT," + KEY_PASSWORD
                + " TEXT" + ")";
        Log.d("debug",CREATE_USER_TABLE);
        db.execSQL(CREATE_USER_TABLE);
    }

I want to add another column that adds data but my app crashes when I do that. I added score INT, and I changed the methods so that along with adding a password and username to the user, I can add a score too, but it doesn't work. What is the best way of making that work?

Also, I wish to add another table that stores images and I want to link the images to each user in the other table. How do I go about that?

hichris123
  • 10,145
  • 15
  • 56
  • 70
shreyashirday
  • 892
  • 1
  • 10
  • 34
  • Can you add the code that add a column into table ? Also the error log of application crush. – Lucifer Mar 03 '14 at 04:33
  • pls post the code that you use 1. in your activity(eg. in the above code, if its onCreate of your activity, nothing else is declared..layout etc One of your errors could be that too) 2. in the db helper 3. Logcat when the app crashes – Pararth Mar 03 '14 at 04:35
  • See: http://stackoverflow.com/questions/21881992/when-is-sqliteopenhelper-oncreate-onupgrade-run – laalto Mar 03 '14 at 06:31

2 Answers2

3

As you upgrade your database by adding some table or columns into the table at that time its necessary that you also change or upgrade at that time its required to change version of your database also.

If you will update or add any columns directly and try to run then it will not be reflected to your database at all.

So make sure you always upgrade the version and uninstall the older version of your database and then check by installing the upgraded version.

Each time it is called, it will check to see if this database and version already exist. If the database doesn’t exist, it will create the database by calling onCreate(), and it will store the database name and version number with it. If it does exist, but the version of the current version is lower than what is defined in DATABASE_VERSION, the onUpgrade() method will be called.

GrIsHu
  • 29,068
  • 10
  • 64
  • 102
1

As the table already exists, you can not use Create Table unless you delete it first, or you can use an alter table command.

See How to add new Column to Android SQLite Database?

Community
  • 1
  • 1
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64