问题描述
|
我正在尝试开发一个Android应用程序,允许用户使用其名称和密码登录数据库。
我在以下链接上尝试了解决方案:
使用sqlite验证Android中的登录名
它对我有用,但问题是,只有通过编辑清单文件并将登录活动替换为其他活动,代码才能进入项目。
我的意思是,当项目在其DDMS中创建数据库时,它可以工作。否则,它永远不允许在任何新的仿真器和设备上登录。
我正在使用Assets文件夹中的数据库表。
我的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,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,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,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();
}
请帮我..
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)