-1

I'm trying to make simple android SQLite register but when i try to save data the app crash

Database code:

Image

public class RegistrationDB extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "registration.db";
    private static final String TABLE_NAME = "profile";
    private static final String COLUMN_ID = "id";
    private static final String COLUMN_EMAIL = "email";
    private static final String COLUMN_USERNAME = "username";
    private static final String COLUMN_PASSWORD = "password";


    private static final String CREATE_TABLE =
            "CREATE TABLE " + TABLE_NAME + " (" +
                    COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    COLUMN_USERNAME + " TEXT, "+
                    COLUMN_EMAIL + " TEXT, " +
                    COLUMN_PASSWORD + " TEXT, " + ")";

    private String DROP_TAPLE = "DROP TABLE IF EXISTS " + TABLE_NAME;

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

    }


    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL(CREATE_TABLE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
        sqLiteDatabase.execSQL(DROP_TAPLE);
        onCreate(sqLiteDatabase);

    }

    public long addUser(String email, String username, String password) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();

        values.put(COLUMN_EMAIL, email); // Email
        values.put(COLUMN_USERNAME, username); // username
        values.put(COLUMN_PASSWORD, password); // password

        // Inserting Row
//        long id = db.insert(TABLE_NAME, null, values);
        db.insert(TABLE_NAME, null, values);
        db.close(); // Closing database connection
        return 0;
    }

}

I'm sorry but I get error when I try to type code here that's why I use image

kRiZ
  • 2,320
  • 4
  • 28
  • 39
AhmedEssam
  • 113
  • 1
  • 6

2 Answers2

3

I think there is problem in your query of create table:

COLUMN_PASSWORD + " TEXT, " + ")";

there should not be a comma "," after TEXT if its the end of the query. Remove that comma. Try if it works.

Trupti Nasit
  • 177
  • 2
  • 15
-1

Change

private static final String COLUMN_ID = "id";

To

private static final String COLUMN_ID = "_id";

And also change..

private static final String CREATE_TABLE =
            "CREATE TABLE " + TABLE_NAME + " (" +
                    COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    COLUMN_USERNAME + " TEXT, "+
                    COLUMN_EMAIL + " TEXT, " +
                    COLUMN_PASSWORD + " TEXT, " + ")";

To

private static final String CREATE_TABLE =
            "CREATE TABLE " + TABLE_NAME + " (" +
                    COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    COLUMN_USERNAME + " TEXT, "+
                    COLUMN_EMAIL + " TEXT, " +
                    COLUMN_PASSWORD + " TEXT)";

When this is done, clear app data, or uninstall and reinstall and then run again.

itaintme
  • 1,575
  • 8
  • 22