我正在尝试从用户那里获取登录信息,然后检查数据库以查找该信息,并且如果该信息存在于数据库中,则该程序会将用户引导至另一个页面,否则会烘烤“错误信息”。但是我发现android无法从用户表数据库中读取信息。我不得不提到数据库文件夹将在数据文件夹中创建,并且我可以在该文件夹中看到我的数据库文件。请帮助我解决我的问题。这是我编写的所有代码: ExternalDataBase类:
public class ExternalDataBase extends SQLiteOpenHelper {
private String db_path = null;
private static String db_name = "testdb.db";
public static final String tbl_user = "Tbl_Users";
public static final String tbl_product = "Tbl_Product";
//TABLE_CONTENT
private SQLiteDatabase sqLiteDatabase;
private final Context context;
/*
constructor taks and keeps a reference of the passed context in order to
access to the application assets and resources.
*/
@SuppressLint("SdCardPath")
public ExternalDataBase(Context context) {
super(context, db_name, null, 1);
this.context = context;
if (android.os.Build.VERSION.SDK_INT >= 17) {
db_path = context.getApplicationInfo().dataDir + "/databases/";
Toast.makeText(context, "ana", Toast.LENGTH_LONG).show();
} else {
db_path = "/data/data/" + context.getPackageName() + "/databases/";
Toast.makeText(context, "arta", Toast.LENGTH_LONG).show();
}
}
/* Create a empty database on the system and rewrites it with your own database */
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
//do nothing, database is already exist
} else {
/* By calling this method and empty database will be created into the default system path.
of your application so we are gonna be able to overwrite that database with our database.*/
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = db_path + db_name;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY
| SQLiteDatabase.NO_LOCALIZED_COLLATORS
| SQLiteDatabase.CREATE_IF_NECESSARY);
} catch (SQLiteException e) {
//database doesn't exist yet
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null;
}
/* copies your database from your local assets-folder to the just created empty database in the system
* folder, from where it can be accessed and handled. This is done by transfering bytestream.*/
public void copyDataBase() throws IOException {
//Open your local db as the input stream
InputStream myInput = context.getAssets().open(db_name);
//path to the just created empty db
String outFileName = db_path + db_name;
//open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the input file to the output file
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
//close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException {
// open the database
String myPath = db_path + db_name;
//SQLiteDatabase.NO_LOCALIZAED_COLLATORS
sqLiteDatabase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY
| SQLiteDatabase.NO_LOCALIZED_COLLATORS
| SQLiteDatabase.CREATE_IF_NECESSARY);
}
@Override
public synchronized void close() {
if (sqLiteDatabase != null)
sqLiteDatabase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
UserAdapter.java类:
public class UserDbAdapter extends ExternalDataBase {
public static String KEY_ID = "id";
public static String KEY_USERNAME = "username";
public static String KEY_PASSWORD = "password";
/* Constructor take and keeps a reference of the passed context in order to access
* to the application assets and resources.*/
public UserDbAdapter(Context context) {
super(context);
}
public long Insert(Users users) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_USERNAME, users.getUsername());
contentValues.put(KEY_PASSWORD, users.getPassword());
return db.insertOrThrow(tbl_user, null, contentValues);
}
public int Login(Users users) {
int res = 0;
SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
Cursor cursor = sqLiteDatabase.rawQuery("select * from "+tbl_user+" where "+KEY_USERNAME +"='"+users.getUsername()+"' and "+
" "+KEY_PASSWORD+"='"+users.getPassword()+"' ",null);
if (cursor.moveToNext()) {
res = 1;
} else {
res = 0;
}
cursor.close();
return res;
}
}
用户类别:
public class Users {
private int Id;
private String Username;
private String Password;
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
public String getUsername() {
return Username;
}
public void setUsername(String username) {
Username = username;
}
public String getPassword() {
return Password;
}
public void setPassword(String password) {
Password = password;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
ExternalDataBase externalDatabase;
UserDbAdapter userDbAdapter;
EditText txt_username,txt_password;
Button btn_enter;
public int result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt_username = findViewById(R.id.txt_username);
txt_password = findViewById(R.id.txt_password);
btn_enter = findViewById(R.id.btn_enter);
externalDatabase = new ExternalDataBase(getApplicationContext());
LoadingDatabase();
userDbAdapter = new UserDbAdapter(getApplicationContext());
btn_enter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = txt_username.getText().toString();
String password = txt_password.getText().toString();
Users users = new Users();
users.setPassword(username);
users.setUsername(password);
result = userDbAdapter.Login(users);
if(result == 1){
Intent intent = new Intent(getApplicationContext(),Main2Activity.class);
startActivity(intent);
}else{
Toast.makeText(getApplicationContext(),"Wrong information",Toast.LENGTH_LONG).show();
}
}
});
}
private void LoadingDatabase() {
try{
externalDatabase.createDataBase();
}catch (IOException e){
e.printStackTrace();
}
externalDatabase.openDataBase();
}
}