0

So my SQLite INSERT INTO function is inserting data in the wrong order even though I have put the contentValues in the right order.

This is my class:

public class UserInfo extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 2;
    private static final String DB_TABLE_NAME = "usersData";
    private static final String USERNAME = "username";
    private static final String DETAILS = "details";
    public static final String DATABASE_NAME = "users.db";
    private static final String DICTIONARY_TABLE_CREATE =
            "CREATE TABLE " + DB_TABLE_NAME + " (" +
                    USERNAME  + " TEXT," + DB_ONE + " TEXT" + " )";

    public ReminderDB(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        Log.d("Database operations", "Database created");
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DICTIONARY_TABLE_CREATE);
        Log.d("Database operations", "Table created");
    }

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

    public void addReminder(String username, String reminders){
        SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(USERNAME, username);
        contentValues.put(DETAILS, details);
        sqLiteDatabase.insert(DB_TABLE_NAME, null, contentValues);
    }
}

LogCat error stack:

android.database.sqlite.SQLiteException: table userData has no column named username (code 1): , while compiling: INSERT INTO userData(details,username) VALUES (?,?)
  • 1
    Please see this tutorial: http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/ – Avishek Das Mar 04 '16 at 10:09
  • I recommend you starting using `greenDAO` and forget about handling `SQL` manually. (I know it's oftopic) – giannisf Mar 04 '16 at 10:09
  • Your second column name is created by a variable `DB_ONE` in your create table query. I cannot see that variable in your class. What's the value of it? – Prerak Sola Mar 04 '16 at 10:12
  • The code and error message do not agree. It's not this code that causes the problem. Anyway, the order of params in `ContentValues` does not matter and internally it's a `Map` without any specific iteration order. – laalto Mar 04 '16 at 10:13
  • Probably you added this column `username` at later stage. Try unistalling your app and installing again. – Rohit5k2 Mar 04 '16 at 10:13
  • @giannisf no need to use any ORM.. it'll have more time complexity than inserting data manually with SQLiteStatement – Sanket Kachhela Mar 04 '16 at 10:14
  • 1
    @SanketKachhela Yeah I know, but it makes so easy your life, especially in the long term :) – giannisf Mar 04 '16 at 10:16

1 Answers1

1

android.database.sqlite.SQLiteException: table userData has no column named username (code 1): , while compiling: INSERT INTO userData(details,username) VALUES (?,?)

You have a table named "userData" in your Local Database. But doesn't have a column called "username".

Don't

 private static final String DB_TABLE_NAME = "usersData";

Do

 private static final String DB_TABLE_NAME = "userData";

Uninstall & Run Again .

Go Through Android SQLite Database Tutorial

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198