如何从SQLite数据库获取用户Id(Rowid)并在TextView(Android)中显示
我做了一个SQLite登录和注册应用程序。成功登录后,我希望用户看到一个文本视图,其中他/她看到“欢迎用户\名称”和“您的Id号:用户\ Id”。通过用户\ Id,我希望看到RowId 以下是数据库助手的一些代码:如何从SQLite数据库获取用户Id(Rowid)并在TextView(Android)中显示,android,database,sqlite,login,rowid,Android,Database,Sqlite,Login,Rowid,我做了一个SQLite登录和注册应用程序。成功登录后,我希望用户看到一个文本视图,其中他/她看到“欢迎用户\名称”和“您的Id号:用户\ Id”。通过用户\ Id,我希望看到RowId 以下是数据库助手的一些代码: public class DatabaseHelper extends SQLiteOpenHelper { private static final int DATABASE_VERSION = 1; private static final String DATABASE_N
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;
}
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);
在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();
}
最后,我希望它显示在用户活动中:
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;
}
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号:null”
我在网上找不到任何解决方案,所以我把这个贴在这里。
如何获取用户id(必须是整数主键自动递增)并将其显示为字符串。但为了使其更为复杂,我还需要用户id作为int/long来执行进一步的函数(转换为二进制代码等)。尝试以下方法:
替换
cursor.getLong(0);
与
Upd.:
另外,请尝试删除以下内容:
Intent idIntent = new Intent(activity, UsersActivity.class);
替换
idIntent.putExtra("ID", ID);
与
试试这个:
替换
cursor.getLong(0);
与
Upd.:
另外,请尝试删除以下内容:
Intent idIntent = new Intent(activity, UsersActivity.class);
替换
idIntent.putExtra("ID", ID);
与
您的DatabaseHelper有一些bug和不相关的代码结构 替换DatabaseHelper,如下代码所示
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);
}
}
}
您必须像这样在活动中调用DatabaseHelper
private DatabaseHelper databaseHelper;
databaseHelper = new DatabaseHelper(context.getApplicationContext());
databaseHelper.open();
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();
}
像这样从本地数据库获取值
private DatabaseHelper databaseHelper;
databaseHelper = new DatabaseHelper(context.getApplicationContext());
databaseHelper.open();
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();
}
您的DatabaseHelper有一些bug和不相关的代码结构 替换DatabaseHelper,如下代码所示
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);
}
}
}
您必须像这样在活动中调用DatabaseHelper
private DatabaseHelper databaseHelper;
databaseHelper = new DatabaseHelper(context.getApplicationContext());
databaseHelper.open();
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();
}
像这样从本地数据库获取值
private DatabaseHelper databaseHelper;
databaseHelper = new DatabaseHelper(context.getApplicationContext());
databaseHelper.open();
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();
}
为什么在getUserId()方法中使用可写数据库而不是可读数据库?@Hetfieldan24我尝试了这两种方法,但都不起作用。当使用
DatabaseUtils#dumpCursor()时,您看到了什么
method?检查我的答案。如我所见,您只是将id放在了错误的意图中,而您并没有启动它。@pskink I/System.out:>>>>>>>转储游标android.database.sqlite。SQLiteCursor@5f0694cI/System.out:0{I/System.out:user_id=1 I/System.out:}I/System.out:为什么在getUserId()中获得可写数据库而不是可读数据库方法?@Hetfieldan24我尝试了这两种方法,但都不起作用。当你使用DatabaseUtils#dumpCursor()
方法时,你会看到什么?检查我的答案。正如我所看到的,你只是把id放在了错误的意图中,而你并没有启动它。@pskink I/System.out:>>>>>Dumping cursor android.database.sqlite。SQLiteCursor@5f0694cI/System.out:0{I/System.out:user_id=1 I/System.out:}I/System.out:非常感谢。我根本没有想过结构。在一个视频中,它是这样显示的,所以我只是保留了它。但是无论如何,我会更改它以改进应用程序。非常感谢。我根本没有想过结构。在一个视频中,它是这样显示的,所以我只是保留了它。但无论如何,我不会更改它o改进应用程序。