我进行了sqlite登录并注册应用程序。成功登录后,我希望用户看到他/她看到" Welcome user_name "one_answers"您的ID-number: user_id 。以及 user_id的文本视图。我想看rowid。
这是 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;
}
在 loginactivity 中,我希望在单击登录后获取用户名和用户ID的值。这是一些代码:
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();
}
最后,我希望将其显示在 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);
但是,使用用户名,一切都可以正常工作,但是用户ID被弄乱了。它向我展示了"您的ID-number:null"
我在网上找不到任何解决方案,所以我在此处发布。我如何获得用户ID,该用户ID必须是整数主键自动启动并显示为字符串。但是,为了使其更加复杂,我还需要用户ID作为INT/long来进行进一步的功能(转换为二进制代码,...)。
您的数据库Helper有一些错误和无关紧要的代码结构。
替换您的数据库Helper,如下面的代码。
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);
}
}
}
您必须像这样的活动中调用数据库helper
private DatabaseHelper databaseHelper;
databaseHelper = new DatabaseHelper(context.getApplicationContext());
databaseHelper.open();
从您的本地DB获取值
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();
}
尝试以下:
替换
cursor.getLong(0);
cursor.getLong(cursor.getColumnIndex(COLUMN_USER_ID));
upd。:也尝试删除此问题:
Intent idIntent = new Intent(activity, UsersActivity.class);
并更换
idIntent.putExtra("ID", ID);
accountIntent.putExtra("ID", ID);