0

I use this code for saving User object into SQLite, But I get this error. What's problem and solution?

Log

11-12 13:48:18.647: E/AndroidRuntime(2409): Caused by: android.database.sqlite.SQLiteException:               no such column: firstname (code 1): , 
while compiling: SELECT id, firstname, lastname, phonenumber, groupname FROM Users WHERE  id = ?

Code

MySQLiteHelper sql=new MySQLiteHelper(MainActivity.this, "users");
for(int i=0;i<5;i++)
sql.addUser(new User());

for(int i=1;i<=5;i++)
    Log.i(i+"", sql.getUser(i).toString());

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public class MySQLiteHelper extends SQLiteOpenHelper {

    // Database Version
    private static final int DATABASE_VERSION = 2;
    // Database Name
    private static String TABLE_NAME = "Users";

    public static String DB_NAME;
    public static String DB_PATH;
    private Context myContext;
    SQLiteDatabase db1;




    public MySQLiteHelper(Context context,String Databasename) {
        super(context, Databasename, null, DATABASE_VERSION); 
        myContext=context;

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // SQL statement to create User table
        String CREATE_User_TABLE = "CREATE TABLE " + TABLE_NAME + " (id " +
                "INTEGER PRIMARY KEY AUTOINCREMENT, firstname TEXT,lastname TEXT,phonenumber TEXT,groupname TEXT)";

        // create Users table
        db.execSQL(CREATE_User_TABLE);
    }


    // Users table name

    // Users Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_FIRSTNAME = "firstname";
    private static final String KEY_LASTNAME = "lastname";
    private static final String KEY_PHONENUMBER = "phonenumber";
    private static final String KEY_GROUPNAME = "groupname";

    private static final String[] COLUMNS = {KEY_ID,KEY_FIRSTNAME,KEY_LASTNAME,KEY_PHONENUMBER,KEY_GROUPNAME};


    public void addUser(User user){

        // 1. get reference to writable DB
        SQLiteDatabase db = this.getWritableDatabase();

        // 2. create ContentValues to add key "column"/value
        ContentValues values = new ContentValues();

 values.put(KEY_FIRSTNAME, user.getFirstName());
 values.put(KEY_LASTNAME, user.getLastName());
 values.put(KEY_PHONENUMBER, user.getPhoneNumber());
 values.put(KEY_GROUPNAME, user.getGroupName());
        // 3. insert
        db.insert(TABLE_NAME, // table
                null, //nullColumnHack
                values); // key/value -> keys = column names/ values = column values

        // 4. close
        db.close();
    }

    public User getUser(int id){

        // 1. get reference to readable DB
        SQLiteDatabase db = null ;

        db = this.getReadableDatabase();

        // 2. build query
        Cursor cursor =
                db.query(TABLE_NAME, // a. table
                COLUMNS, // b. column names
                " id = ?", // c. selections
                new String[] { String.valueOf(id) }, // d. selections args
                null, // e. group by
                null, // f. having
                null, // g. order by
                null); // h. limit

        // 3. if we got results get the first one
        if (cursor != null)
            cursor.moveToFirst();

        System.gc();
        // 4. build User object
        User user=new User();
        user.setId(Integer.parseInt(cursor.getString(0)));
        user.setFirstname(cursor.getString(1));
        user.setLastname(cursor.getString(2));
        user.setPhoneNumber(cursor.getString(3));
        user.setGroupName(cursor.getString(4));

        cursor.close();
        return user;
    }




     @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older Users table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);

    // create fresh Users table
    this.onCreate(db);
}





    @Override
    public synchronized void close() {
        if(db1!=null)db1.close();
        //super.close();

    }


}
sdaasd
  • 13
  • 6

1 Answers1

0

I suspect your problem is that you had a previous iteration of the table that didn't get updated. I see that you are using version 2. You either need to provide update instructions in the onUpgrade function, or else blow away your database and try again. If there isn't any important information, I would recommend clearing the program memory, which includes all databases, and see if it works like that.

EDIT: Your onUpgrade works fine, but I suspect that there was some iteration in your code. You could increment the version, or do as I suggested above.

PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142
  • 1
    The method looks fine, but if you had an iteration that didn't work on the first time, it could still be invalid. Try bumping your db version to 3. – PearsonArtPhoto Nov 12 '14 at 19:19