大家好我是Java和SQLite编程方面的新手,我从SQLite数据库中获取和检索特定数据时遇到了问题, 我想创建一个登录程序,用户将输入用户名和密码(LoginScreen.java),它将检查数据库中的现有数据(database.java),如果存在,它将返回true给LoginScreen.java。
之后,LoginScreen.java将检查,如果为true,它将运行SQLViews Intent,如果为false,它将运行Registration Intent。
问题是,即使数据存在于数据库中,我的代码也总是返回false。 你能帮我检查一下我的代码吗?
谢谢,
以下是代码: Database.java
package com.thesis.teamizer;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class Database {
public static final String DATABASE_NAME = "TeamizerDB.db";
public static final String TABLE_MEMBER = "Member";
public static final int DATABASE_VERSION = 1;
public static final String MEMBER_USERNAME = "Username";
public static final String MEMBER_PASSWORD = "Password";
public static final String MEMBER_EMAIL = "Email";
public DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
public static class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + TABLE_MEMBER + " (" + MEMBER_USERNAME
+ " TEXT PRIMARY KEY NOT NULL, " + MEMBER_PASSWORD
+ " TEXT NOT NULL, " + MEMBER_EMAIL + " TEXT NOT NULL);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + TABLE_MEMBER);
onCreate(db);
}
}
public Database(Context c) {
ourContext = c;
}
public Database open() throws SQLException {
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close() {
ourHelper.close();
}
public boolean createEntry(String username, String password, String email) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(MEMBER_USERNAME, username);
cv.put(MEMBER_PASSWORD, password);
cv.put(MEMBER_EMAIL, email);
ourDatabase.insert(TABLE_MEMBER, null, cv);
return true;
}
public String getData() {
// TODO Auto-generated method stub
String[] columns = new String[] { MEMBER_USERNAME, MEMBER_PASSWORD,
MEMBER_EMAIL };
Cursor c = ourDatabase.query(TABLE_MEMBER, columns, null, null, null,
null, null);
String result = "";
int iUsername = c.getColumnIndex(MEMBER_USERNAME);
int iPassword = c.getColumnIndex(MEMBER_PASSWORD);
int iEmail = c.getColumnIndex(MEMBER_EMAIL);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
result = result + c.getString(iUsername) + " "
+ c.getString(iPassword) + " " + c.getString(iEmail) + "\n";
}
return result;
}
public Boolean getName(String l) throws SQLException {
// TODO Auto-generated method stub
String Query = "SELECT USERNAME FROM " + TABLE_MEMBER + " WHERE "
+ MEMBER_USERNAME + " =?";
Cursor mCursor = ourDatabase.rawQuery(Query, new String[] { l });
boolean hasObject = false;
if (mCursor.moveToFirst()) {
hasObject = false;
mCursor.close();
}
else
hasObject = true;
return hasObject;
}
public Boolean getMatched(String username, String password) {
// TODO Auto-generated method stub
String Query = "SELECT USERNAME FROM " + TABLE_MEMBER + " WHERE "
+ MEMBER_USERNAME + " =? AND " + MEMBER_PASSWORD + " =?";
Cursor c = ourDatabase.rawQuery(Query, new String[] { username,
password });
if (c.moveToFirst()) {
return true;
} else {
return false;
}
}
}
LoginScreen.java
package com.thesis.teamizer;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class LoginScreen extends Activity {
private EditText etUsername;
private EditText etPassword;
private Button bLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.login_screen);
validationUsernameAndPassword();
ifRegisterButtonClicked();
}
private void validationUsernameAndPassword() {
// TODO Auto-generated method stub
etUsername = (EditText) findViewById(R.id.etLoginUsername);
etPassword = (EditText) findViewById(R.id.etLoginPassword);
bLogin = (Button) findViewById(R.id.bLogin);
final String username = etUsername.getText().toString();
final String password = etPassword.getText().toString();
bLogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int flag = 0;
/*
* if (!isValid(username)) {
* etUsername.setError("Username must be filled"); flag++; }
*
* if (!isValid(password)) {
* etPassword.setError("Password must be filled"); flag++; }
*/
if (flag == 0) {
Database checkLogin = new Database(LoginScreen.this);
checkLogin.open();
Boolean temp = checkLogin.getMatched(username, password);
// Kalo username n passwordnya benar2 ada di database
if (temp == true) {
Intent intent = new Intent(
"com.thesis.teamizer.SQLVIEWS");
startActivity(intent);
} // Kalo username n passwordnya salah atau gak ada di
// database
if (temp == false) {
Intent i = new Intent("com.thesis.teamizer.REGISSCREEN");
startActivity(i);
}
checkLogin.close();
}
}
private boolean isValid(String temp) {
if (temp != null && temp.length() > 6 && !temp.contains(" ")) {
return true;
}
return false;
}
});
}
private void ifRegisterButtonClicked() {
// TODO Auto-generated method stub
Button bRegis = (Button) findViewById(R.id.bRegister);
bRegis.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent("com.thesis.teamizer.REGISSCREEN");
startActivity(intent);
}
});
}
}
答案 0 :(得分:0)
private void validationUsernameAndPassword() {
...
final String username = etUsername.getText().toString();
final String password = etPassword.getText().toString();
bLogin.setOnClickListener(new OnClickListener() {
...
您正在创建活动时读取值。 您应该在单击按钮后执行此操作。