Search code examples
javaandroidsqliteandroid-sqlite

Connecting my app to a SQLite database - how to?


I am creating a login app, where a user has an username and a password. I want to save sign up data in my database, but when I want to get access to the elements from database, no one seems to be saved ( it has no user saved in db). I suppose the error comes from db implementation. Any idea?

public class DataBaseHelper extends SQLiteOpenHelper {

public static final String TABLE_NAME = "user_table";
public static final String COL1 = "ID";
public static final String COL2 = "user_username";
public static final String COL3 = "user_password";

public DataBaseHelper(Context context) {
    super(context, TABLE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String createTable = "CREATE TABLE " + TABLE_NAME + "( " + COL2 + " TEXT," +
            COL3 + "TEXT)";
    db.execSQL(createTable);
}

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

public void add(String name, String password) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL2, name);
    contentValues.put(COL3, password);
    db.insert(TABLE_NAME, null, contentValues);
}

public Cursor getData() {
    SQLiteDatabase db = this.getWritableDatabase();
    String query = "SELECT *FROM " + TABLE_NAME;
    Cursor data = db.rawQuery(query, null);
    return data;
}

LOGIN Activity:

public void loginSeach(View view) {
    EditText password_editText = (EditText) findViewById(R.id.editText_password);
    EditText username_editText = (EditText) findViewById(R.id.editText_username);
    String currUserName = username_editText.getText().toString();
    String currPassWord = password_editText.getText().toString();
    DataBaseHelper db = new DataBaseHelper(this);
    Cursor data = db.getData();
    int Number = 0;
    while (data.moveToNext()) {
        Number ++;
        String userName = data.getString(1);
        String passWord = data.getString(2);
    }

    Toast.makeText(this, Integer.toString(Number), Toast.LENGTH_SHORT).show();

}

Sign up activity:

public void createAccount(View view) {
    EditText usernameEditText = (EditText) findViewById(R.id.editText_createUsername);
    EditText passwordEditText = (EditText) findViewById(R.id.editText_createPassword);
    String userName = usernameEditText.getText().toString();
    String passWord = passwordEditText.getText().toString();

    dataBaseHelper.add(userName, passWord);

    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);

}

Solution

  • This statement in onCreate():

    String createTable = "CREATE TABLE " + TABLE_NAME + "(" + COL2 + " TEXT," + COL3 + "TEXT)";
    

    created a table without an ID column.
    Uninstall the app from the emulator/device, change your code to this:

    String createTable = "CREATE TABLE " + TABLE_NAME + "(" + COL1 + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL2 + " TEXT," + COL3 + " TEXT)";
    

    and rerun the app.