我正在尝试创建用户可以在任何网站上管理她的密码的密码管理器应用程序。
用户需要填写 3 个字段-Website
和。在幕后,数据应该在 SQLite 中插入数据库,但我认为数据库没有打开,因为我看不到路径中的数据库文件夹:Username
Password
/data/data/MY_APP/
这是MySQLiteHelper
: http: //pastebin.com/4ZWJuS5W
package db_pkg;
import java.io.UTFDataFormatException;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.util.Log;
public class MySQLiteHelper
{
public static final String DB_NAME = "mu_dbname";
public static final String TASKS_TABLE = "passwords";
public static final int DB_VER = 1;
public static final String TASK_ID = "_id";
public static final String TASK_WEB = "web";
public static final String TASK_USER = "user";
public static final String TASK_PASSWORD = "password";
// public static final String TASK_LAT = "lat";
// public static final String TASK_LNG = "lng";
private static final String SCRIPT_CREATE_DB = "create table "
+ TASKS_TABLE + "(" + BaseColumns._ID
+ " integer primary key autoincrement, " + TASK_WEB
+ " text not null, " + TASK_USER + " text not null, "
+ TASK_PASSWORD + " text not null" + ");";
private Context context;
private MyDBHelper myDBHelper;
public MySQLiteHelper(Context context)
{
this.context = context;
this.myDBHelper = new MyDBHelper(this.context);
}
public void addTaskItem(addPassword item)
{
SQLiteDatabase database = myDBHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(TASK_WEB, item.getWebsite());
values.put(TASK_USER, item.getUsername());
values.put(TASK_PASSWORD, item.getPassword());
database.insert(TASKS_TABLE, null, values);
database.close();
}
public boolean updateItemById(int taskID, addPassword item)
{
SQLiteDatabase database = myDBHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(TASK_WEB, item.getWebsite());
values.put(TASK_USER, item.getUsername());
values.put(TASK_PASSWORD, item.getPassword());
boolean b = database.update(TASKS_TABLE, values,
TASK_ID + "=" + taskID, null) > 0;
database.close();
return b;
}
public boolean deleteTaskItemById(int taskID)
{
SQLiteDatabase database = myDBHelper.getWritableDatabase();
boolean b = database.delete(TASKS_TABLE, TASK_ID + "=" + taskID, null) > 0;
return b;
}
public Cursor getCursorALL()
{
Cursor cursor;
SQLiteDatabase database = myDBHelper.getReadableDatabase();
cursor = database.query(TASKS_TABLE,
new String[] { TASK_ID,TASK_WEB,TASK_USER,TASK_PASSWORD},
null,null, null, null, null);
return cursor;
}
//testing Debugging
public void printAllCursorDB()
{
Cursor cursor=getCursorALL();
cursor.moveToFirst();
while(!cursor.isAfterLast())
{
StringBuffer st=new StringBuffer();
st.append("ID:"+cursor.getString(0));
st.append(" Text:"+cursor.getString(1));
st.append(" Phone:"+cursor.getString(2));
st.append(" Priority:"+cursor.getString(3));
Log.d("MyTasksDBMngr", st.toString());
cursor.moveToNext();
}
cursor.close();
}
private class MyDBHelper extends SQLiteOpenHelper
{
public MyDBHelper(Context context)
{
super(context, DB_NAME, null, DB_VER);
Log.i("MyDBHelper", "Constructor");
}
@Override
public void onCreate(SQLiteDatabase db)
{
String sqlst = String.format("drop table if exists %s;",
TASKS_TABLE);// משפט למחיקת הטבלה
db.execSQL(sqlst);
db.execSQL(SCRIPT_CREATE_DB);
Log.i("MyDBHelper", "onCreate");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
onCreate(db);
}
}
}
这是addPassword
:
http: //pastebin.com/e17CQ6mF
package db_pkg;
public class addPassword {
String website, username, password;
int id;
public addPassword()
{
}
public addPassword(String website, String username, String password, int id) {
this.website = website;
this.username = username;
this.password = password;
this.id = id;
}
public addPassword(String website, String password, int id) {
this.website = website;
this.id = id;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getWebsite() {
return website;
}
public void setWebsite(String web) {
if(web.length() < 2)
{
this.website = web;
}
}
public String getUsername() {
return username;
}
public void setUsername(String user) {
this.username = user;
}
public String getPassword() {
return password;
}
public void setPassword(String pass) {
this.password = pass;
}
}
这是我的Activity
:
package com.appweb.passwordmanager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import db_pkg.MySQLiteHelper;
import db_pkg.addPassword;
public class newPassword extends AppCompatActivity {
private MySQLiteHelper niv;
private boolean isEmpty(EditText etText) {
if (etText.getText().toString().trim().length() > 0) {
return false;
} else {
return true;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_password);
final EditText website = (EditText) findViewById(R.id.website);
final EditText username = (EditText) findViewById(R.id.username);
final EditText password = (EditText) findViewById(R.id.password);
Button add = (Button) findViewById(R.id.add);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!isEmpty(password))
{
if(!isEmpty(website))
{
addPassword item = new addPassword(website.getText().toString(),
username.getText().toString(),
password.getText().toString(),
0);
niv.addTaskItem(item);
}
}
}
});
}
}