0

我正在尝试创建用户可以在任何网站上管理她的密码的密码管理器应用程序。

用户需要填写 3 个字段-Website和。在幕后,数据应该在 SQLite 中插入数据库,但我认为数据库没有打开,因为我看不到路径中的数据库文件夹:UsernamePassword/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);
                    }
                }
            }
        });
    }
}
4

1 回答 1

3

@拉姆齐

SQLite是一个开源关系数据库,即用于在 android 设备上执行数据库操作,例如存储、操作或从数据库中检索持久数据。

首先请阅读

  1. 安卓 SQLite 数据库

  2. Android SQLite 数据库教程

例子

  public class MySQLiteHelper extends SQLiteOpenHelper {

  public static final String TABLE_COMMENTS = "comments";
  public static final String COLUMN_ID = "_id";
  public static final String COLUMN_COMMENT = "comment";

  private static final String DATABASE_NAME = "commments.db";
  private static final int DATABASE_VERSION = 1;

  // Database creation sql statement
  private static final String DATABASE_CREATE = "create table "
      + TABLE_COMMENTS + "(" + COLUMN_ID
      + " integer primary key autoincrement, " + COLUMN_COMMENT
      + " text not null);";

  public MySQLiteHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
  }

  @Override
  public void onCreate(SQLiteDatabase database) {
    database.execSQL(DATABASE_CREATE);
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.w(MySQLiteHelper.class.getName(),
        "Upgrading database from version " + oldVersion + " to "
            + newVersion + ", which will destroy all old data");
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_COMMENTS);
    onCreate(db);
  }
于 2015-10-30T10:57:49.350 回答