我正在尝试开发一个Android应用程序,让用户使用他们的名称和密码登录已存在于数据库中。 我在以下链接上尝试了解决方案:
Using SQlite to validate Logins in Android
它适用于我,但问题是代码只有在我通过编辑Manifest文件并通过其他一些活动替换登录活动进入项目时才能工作。 我的意思是当项目在其DDMS中创建数据库时它可以工作。否则,它永远不允许在任何新的模拟器和设备上登录。 我在资源文件夹中使用我的数据库表。
我的SqlHelper类是:
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 (dbSqlite != null)
dbSqlite.close();
super.close();
}
}
我的DatabaseMetaData接口类是:
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";
}
在LoginActivity中,我使用以下方法:
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();
}
请帮帮我..