DBAdapter.java
public void onCreate(SQLiteDatabase db) {
db.execSQL(" CREATE TABLE " + DATABASE_TABLE + " (" + KEY_FIRSTNAME
+ " TEXT NOT NULL, " + KEY_MIDDLENAME + " TEXT NOT NULL, "
+ KEY_LASTNAME + " TEXT NOT NULL, " + KEY_USERNAME + " TEXT NOT NULL, " + KEY_MAIL_ID + " TEXT NOT NULL, "
+ KEY_PASSWORD + " TEXT NOT NULL, " + KEY_CONFIRM
+ " TEXT NOT NULL, " + KEY_DATE_OF_BIRTH
+ " INTEGER NOT NULL "
+ ");");
public long insertRecord(String firstname, String middlename,
String lastname, String username, String mail_id, String pass_word,
String re_password, String d_o_b) {
ContentValues cv = new ContentValues();
cv.put(KEY_FIRSTNAME, firstname);
cv.put(KEY_MIDDLENAME, middlename);
cv.put(KEY_LASTNAME, lastname);
cv.put(KEY_USERNAME, username);
cv.put(KEY_MAIL_ID, mail_id);
cv.put(KEY_PASSWORD, pass_word);
cv.put(KEY_CONFIRM, re_password);
cv.put(KEY_DATE_OF_BIRTH, d_o_b);
//cv.put(KEY_COUNTRY, country);
return mDB.insert(DATABASE_TABLE, null, cv);
}
public String getSinlgeEntry(String userName) {
Cursor cursor = mDB.query(DATABASE_TABLE,null,KEY_USERNAME+" =?",new String[]{userName},null,null,null);
if (cursor.getCount() < 1) // UserName Not Exist
return "NOT EXIST";
cursor.moveToFirst();
String password = cursor.getString(cursor
.getColumnIndex(KEY_PASSWORD));
return password;
}
MainActivity.java:
dbadapter = new DBAdapter(MainActivity.this);
dbadapter.open();
String stored_password = dbadapter.getSinlgeEntry(username);
Log.i("MainActivity", "The stored password is" + stored_password);
if(password.equals(stored_password))
{
Toast.makeText(MainActivity.this,"It is logging in",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this,Profile_view.class);
startActivity(intent);
}
else
{
Toast.makeText(MainActivity.this,"Either username or password in invalid",Toast.LENGTH_LONG).show();
}
Logcat:
12-06 16:58:14.981: I/RegistrationPage(4527): It has reached onclick
12-06 16:58:14.981: I/RegistrationPage(4527): It has reached the database
12-06 16:58:15.582: I/RegistrationPage(4527): The records are inserted and it is past insertRecord
12-06 16:58:15.594: I/RegistrationPage(4527): Database is closed
12-06 16:58:31.571: I/MainActivity(4527): The stored password isNOT EXIST
12-06 16:58:31.671: I/MainActivity(4527): Database is opened
12-06 17:09:31.023: W/IInputConnectionWrapper(4527): showStatusIcon on inactive InputConnection
12-06 17:09:39.931: D/dalvikvm(4527): GC_EXPLICIT freed 1693K, 9% free 20048K/21959K, paused 13ms+20ms
编辑:
我无法匹配用户名和密码。方法getSingleentry
应该返回密码但它总是返回NOT EXIST,尽管数据库中有条目,游标总是返回NOT EXIST,好像数据库中没有数据一样。
有人可以帮我这个吗?提前致谢。
答案 0 :(得分:1)
public List<UserPojo> getNameList() {
List<UserPojo> contactList = new ArrayList<UserPojo>();
// Select All Query
String selectQuery = "SELECT * FROM Class_Levels";
open();
Cursor cursor = database.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
UserPojo names = new UserPojo();
names.setC_Id(cursor.getString(0));
names.setC_Name(cursor.getString(1));
// Adding contact to list
contactList.add(names);
} while (cursor.moveToNext());
}
cursor.close();
close();
// return contact list
return contactList;
}
这只是一个示例您只需尝试就可以达到您的要求。
答案 1 :(得分:0)
试试这个方法,
public String getSinlgeEntry(String userName) {
Cursor cursor = mDB.query(DATABASE_TABLE,null,KEY_USERNAME+"='" + username +"'",null,null,null);
cursor.moveToFirst();
String password = "";
if (cursor.getCount() > 0)
{
password = cursor.getString(cursor.getColumnIndex(KEY_PASSWORD));
}
return password;
}