Validate login using sqlite database by making objects in Android
I am trying to develop an android application that let users login using their name and password already exists in the database. I tried solution on the following link:
Using SQlite to validate Logins in Android
It works for me but the issue is that the code works only once i get into the project by editing the Manifest file and replacing the login activity by some other activity. I mean it works when the project creates the database in its DDMS. Otherwise it never allows login on any new emulator and device. I am using my database tables in assets folder.
My SqlHelper class is:
public class SqlHelper extends SQLiteOpenHelper implements DatabaseMetaData
{
public static SQLiteDatabase dbSqlite;
protected SQLiteDatabase db = null;
private final Context myContext;
public SqlHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
this.myContext = context;
}
public void onCreate(SQLiteDatabase db) {
// check if exists and copy database from resource
createDB();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w("SqlHelper", "Upgrading database from version " + oldVersion
+ " to " + newVersion + ", which will destroy all old data");
onCreate(db);
}
public void createDatabase() {
createDB();
}
private void createDB() {
boolean dbExist = DBExists();
if (!dbExist) {
copyDBFromResource();
}
}
private boolean DBExists() {
SQLiteDatabase db = null;
try {
String databasePath = DATABASE_PATH + DATABASE_NAME;
db = SQLiteDatabase.openDatabase(databasePath, null,
SQLiteDatabase.OPEN_READWRITE);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
db.setVersion(1);
} catch (SQLiteException e) {
Log.e("SqlHelper", "database not found");
}
if (db != null) {
db.close();
}
return db != null ? true : false;
}
private void copyDBFromResource() {
InputStream inputStream = null;
OutputStream outStream = null;
String dbFilePath = DATABASE_PATH + DATABASE_NAME;
try {
inputStream = myContext.getAssets().open(DATABASE_NAME);
outStream = new FileOutputStream(dbFilePath);
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outStream.write(buffer, 0, length);
}
outStream.flush();
outStream.close();
inputStream.close();
} catch (IOException e) {
throw new Error("Problem copying database from resource file.");
}
}
public void openDataBase() throws SQLException {
String myPath = DATABASE_PATH + DATABASE_NAME;
dbSqlite = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
}
@Override
public synchronized void close() {
if (db开发者_如何学运维Sqlite != null)
dbSqlite.close();
super.close();
}
}
My DatabaseMetaData Interface Class is :
public interface DatabaseMetaData {
String DATABASE_PATH = "/data/data/com.dbApp/databases/";
String DATABASE_NAME = "dbApp";
String TABLE_USERLIST = "UserList";
String TABLE_CUSTOMER = "Customer";
//UserList
String COLUMN_USER_NAME = "User_Name";
String COLUMN_PASSWORD = "Password";
//Customer
String COLUMN_ID = "_id";
String COLUMN_LISTID = "ListID";
String COLUMN_NAME = "Name";
String COLUMN_COMPANYNAME = "CompanyName";
String COLUMN_FIRSTNAME = "FirstName";
String COLUMN_LASTNAME = "LastName";
}
And in LoginActivity i use the following method:
protected boolean tryLogin(String username, String password)
{
try{
Cursor cursor = null;
cursor = db.rawQuery("select " + DatabaseMetaData.COLUMN_PASSWORD + " from " + DatabaseMetaData.TABLE_USERLIST + " where " + DatabaseMetaData.COLUMN_USER_NAME + " = " + "'" +username+ "'", null);
//cursor = db.rawQuery("select password from UserList where user_name='" + username + "'", null);
if (cursor.moveToFirst()) {
String str = cursor.getString(0);
if (password.equals(str)) {
cursor.close();
return true;
}else{
return false;
}
}cursor.close();
}
Please help me..
精彩评论