如何解决通过在Android中制作对象,使用sqlite数据库验证登录
| 我正在尝试开发一个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 举报,一经查实,本站将立刻删除。