I made a SQLite Login and Register app. After a successful Login I want the user to see a TextView where he/she sees "Welcome user_name" and "Your Id-Number: user_id. And by user_id I would like to see the RowId.
Here is some code of the DatabaseHelper:
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "UserManager.db";
private static final String TABLE_USER = "user";
private static final String COLUMN_USER_ID = "user_id";
private static final String COLUMN_USER_NAME = "user_name";
private static final String COLUMN_USER_EMAIL = "user_email";
private static final String COLUMN_USER_PASSWORD = "user_password";
private String CREATE_USER_TABLE = "CREATE TABLE " + TABLE_USER + "("
+ COLUMN_USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_USER_NAME + " TEXT,"
+ COLUMN_USER_EMAIL + " TEXT," + COLUMN_USER_PASSWORD + " TEXT" + ")";
public long getUserId (String email){
long userId = 0;
SQLiteDatabase db = this.getWritableDatabase();
String[] columns = {
COLUMN_USER_ID
};
String selection = COLUMN_USER_EMAIL + " = ?";
String[] selectionArgs = { email };
Cursor cursor = db.query(TABLE_USER,
columns,
selection,
selectionArgs,
null,
null,
null);
int cursorCount = cursor.getCount();
cursor.moveToFirst();
userId = cursor.getLong(0);
cursor.close();
db.close();
if (cursorCount > 0){
return userId;
}
return userId;
}
In the LoginActivity I want the get the value of the user name and the user id after clicking in Login. Here is some code:
if (databaseHelper.checkUser(textInputEditTextEmail.getText().toString().trim()
, textInputEditTextPassword.getText().toString().trim())){
Intent accountsIntent = new Intent(activity, UsersActivity.class);
accountsIntent.putExtra("NAME", databaseHelper.getUserName(textInputEditTextEmail.getText().toString().trim()));
Intent idIntent = new Intent(activity, UsersActivity.class);
String ID = String.valueOf(databaseHelper.getUserId(textInputEditTextEmail.getText().toString().trim()));
idIntent.putExtra("ID", ID);
emptyInputEditText();
startActivity(accountsIntent);
} else {
Snackbar.make(nestedScrollView, getString(R.string.error_valid_email_password), Snackbar.LENGTH_LONG).show();
}
And at the end I want it to be displayed in the UserActivity:
textViewName = (TextView) findViewById(R.id.text1);
String nameFromIntent = getIntent().getStringExtra("NAME");
textViewName.setText("Welcome " + nameFromIntent);
textViewId = (TextView) findViewById(R.id.text2);
String idFromIntent = getIntent().getStringExtra("ID");
textViewId.setText("Your Id-Number: " + idFromIntent);
However, with the user name everything works just fine but the user id is messed up. It shows me "Your Id-Number: null"
I couldn't find any solution online so I post this here. How do I get the user id, which has to be INTEGER PRIMARY KEY AUTOINCREMENT and display it as a string. But to make it even more complicated, I also need the user id as a int/long for further functions (transform to binary code, ...).
Your DatabaseHelper is having some bugs and irrelevant code structure.
Replace Your DatabaseHelper like below code.
public class DatabaseHelper {
private static final String TAG = "DatabaseHelper";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "UserManager";
private static final String TABLE_USER = "user";
private static final String COLUMN_USER_ID = "user_id";
private static final String COLUMN_USER_NAME = "user_name";
private static final String COLUMN_USER_EMAIL = "user_email";
private static final String COLUMN_USER_PASSWORD = "user_password";
private static final String CREATE_USER_TABLE = "CREATE TABLE " + TABLE_USER + "("
+ COLUMN_USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ COLUMN_USER_NAME + " TEXT,"
+ COLUMN_USER_EMAIL + " TEXT,"
+ COLUMN_USER_PASSWORD + " TEXT" + ")";
private final Context context;
private AppDatabaseHelper appDB;
private SQLiteDatabase db;
public DatabaseHelper(Context ctx) {
this.context = ctx;
appDB = new AppDatabaseHelper(context);
}
// Open the database connection.
public DatabaseHelper open() {
db = appDB.getWritableDatabase();
return this;
}
public boolean checkIfUserExit(String tableName,String emailId) {
String where = COLUMN_USER_EMAIL+" LIKE '%"+emailId+"%'";
Cursor c = db.query(true, tableName, null,
where, null, null, null, null, null);
if(c.getCount()>0)
return true;
else
return false;
}
public int GetUserID(String tableName,String emailId) {
String where = COLUMN_USER_EMAIL+" LIKE '%"+emailId+"%'";
Cursor c = db.query(true, tableName, null,
where, null, null, null, null, null);
if(c.getCount()>0)
return c.getInt(0);
else
return 0;
}
public String GetUserUserName(String tableName,String emailId) {
String where = COLUMN_USER_EMAIL+" LIKE '%"+emailId+"%'";
Cursor c = db.query(true, tableName, null,
where, null, null, null, null, null);
if(c.getCount()>0)
return c.getString(1);
else
return null;
}
public static class AppDatabaseHelper extends SQLiteOpenHelper {
AppDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase _db) {
_db.execSQL(CREATE_USER_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading application's database from version " + oldVersion
+ " to " + newVersion + ", which will destroy all old data!");
// Destroy old database:
_db.execSQL("DROP TABLE IF EXISTS " + TABLE_USER);
// Recreate new database:
onCreate(_db);
}
}
}
You have to call DatabaseHelper in Your Activity like this
private DatabaseHelper databaseHelper;
databaseHelper = new DatabaseHelper(context.getApplicationContext());
databaseHelper.open();
Get values from your local DB like this
if (databaseHelper.checkIfUserExit(databaseHelper.TABLE_USER , textInputEditTextEmail.getText().toString().trim())){
Intent accountsIntent = new Intent(activity, UsersActivity.class);
accountsIntent.putExtra("NAME",
databaseHelper.GetUserUserName(databaseHelper.TABLE_USER ,textInputEditTextEmail.getText().toString().trim()));
String ID = String.valueOf(databaseHelper.GetUserID(databaseHelper.TABLE_USER ,textInputEditTextEmail.getText().toString().trim()));
accountsIntent.putExtra("ID", ID);
emptyInputEditText();
startActivity(accountsIntent);
} else {
Snackbar.make(nestedScrollView, getString(R.string.error_valid_email_password), Snackbar.LENGTH_LONG).show();
}