SQLite - UNIQUE constraint failed: (OS error - 2:No such file or directory)

Viewed 2730

I know that this question was already been asked and answered before, but i cannot find any solution for my issue.

I am having an SQLite DB i am trying to create on my Android app. I am parsing my values, everyhting seems ok, but when i try to insert values in the SQLite, i get an UNIQUE constraint failed: (OS error - 2:No such file or directory) error in the insertOrThrow line. Here is my code

public class SQLiteHandler extends SQLiteOpenHelper {

    private static final String TAG = SQLiteHandler.class.getSimpleName();

    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "user_DB";

    // Login table name
    private static final String TABLE_USER = "user";

    // Login Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "name";
    private static final String KEY_SURNAME = "surname";
    private static final String KEY_COUNTRY = "country";
    private static final String KEY_EMAIL = "email";
    private static final String KEY_PASSWORD = "password";
    private static final String KEY_TELEPHONE = "telephone";

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

    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_USER + "(" + KEY_ID + " TEXT PRIMARY KEY,"
                + KEY_NAME + " TEXT," + KEY_SURNAME + " TEXT," + KEY_COUNTRY + " TEXT,"
                + KEY_EMAIL + " TEXT UNIQUE," + KEY_PASSWORD + " TEXT,"
                + KEY_TELEPHONE + " TEXT" + ")";
        db.execSQL(CREATE_LOGIN_TABLE);

        Log.d(TAG, "Database tables created");
    }

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

        // Create tables again
        onCreate(db);
    }

    /**
     * Storing user details in database
     * */
    public void addUser(String id, String name, String surname, String country, String email, String password, String telephone) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_ID, id); // Id
        values.put(KEY_NAME, name); // Name
        values.put(KEY_SURNAME, surname); // Surname
        values.put(KEY_COUNTRY, country); // Country
        values.put(KEY_EMAIL, email); // Email
        values.put(KEY_PASSWORD, password); // Password
        values.put(KEY_TELEPHONE, telephone); // Telephone

        // Inserting Row
        long ide = db.insertOrThrow(TABLE_USER, null, values);
        db.close(); // Closing database connection

        Log.d(TAG, "New user inserted into sqlite: " + ide);
    }


    public String getData(String a){
        SQLiteDatabase db = this.getReadableDatabase();
        String s;
        Cursor c = db.rawQuery("SELECT * FROM " + TABLE_USER, null);
        c.moveToFirst();
        s=c.getString(c.getColumnIndex(a));
        db.close();

        return s;
    }

    /**
     * Re crate database Delete all tables and create them again
     * */
    public void deleteUsers() {
        SQLiteDatabase db = this.getWritableDatabase();
        // Delete All Rows
        db.delete(TABLE_USER, null, null);
        db.close();

        Log.d(TAG, "Deleted all user info from sqlite");
    }

}

The id is AUTOINCREMENT in the MySQL db i use in a server where i get the values. I just want to insert them in a SQLite db in the app. Although i get an exception

android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: user.id (Sqlite code 1555), (OS error - 2:No such file or directory)

What does the No such file or directory means?

0 Answers
Related