vendredi 30 octobre 2015

android database not open - SQLITE

I am trying to create password manager app that the user can manage her password in any websites.

The user need to fill 3 fields - Website, Username and Password. Behind the scenes, the data should be insert to the database in SQLite but I think that the database not open, because I cant see the database folder in path: /data/data/MY_APP/

this is MySQLiteHelper: http://ift.tt/1P1gF1s

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);

        }

    }

}

this is addPassword: http://ift.tt/1Hg9fiR

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;
    }
}

this is my 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);
                    }
                }
            }
        });
    }
}

Aucun commentaire:

Enregistrer un commentaire