androidstudio:sqlite数据库不创建

iq3niunx  于 2021-07-11  发布在  Java
关注(0)|答案(1)|浏览(365)

我目前正在为自己构建一个密码管理器应用程序。我计划将密码保存在sqlite数据库中。为此,我使用以下代码:

package com.example.passwordmanager;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import androidx.annotation.Nullable;

public class DatabaseHelper extends SQLiteOpenHelper {

    public static final String COLUMN_PASSWORD = "password";
    public static final String PASSWORDS_TABLE = COLUMN_PASSWORD + "s_table";
    public static final String COLUMN_ID = "id";
    public static final String COLUMN_SERVICE = "service";
    public static final String COLUMN_CREATION_DATE = "creation_date";
    public static final String COLUMN_USED_NAME = "used_name";
    public static final String COLUMN_USED_EMAIL = "used_email";

    public DatabaseHelper(@Nullable Context context) {
        super(context,  "passwordManager.db", null,1);
        Log.d("DatabaseHelper","Constructor reached");

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String createTableStatement = "CREATE TABLE "+ PASSWORDS_TABLE + " ( " + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_SERVICE + " VARCHAR(40), " + COLUMN_PASSWORD + " VARCHAR(40), " + COLUMN_CREATION_DATE + " VARCHAR(11), " + COLUMN_USED_NAME + " VARCHAR(40), " + COLUMN_USED_EMAIL + " VARCHAR(40), master VARCHAR(20), firstTime BOOLEAN)";
        Log.d("DatabaseHelper","OnCreate1 reached");
        db.execSQL(createTableStatement);
        Log.d("DatabaseHelper","OnCreate2 reached");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        db.execSQL("DROP TABLE IF EXISTS "+PASSWORDS_TABLE);
        onCreate(db);

    }

    public boolean addToTable(Registration profile){

        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues cv = new ContentValues();

        cv.put(COLUMN_PASSWORD,profile.hashedPassword);
        cv.put(COLUMN_SERVICE,profile.service);
        cv.put(COLUMN_CREATION_DATE,profile.creation_date);
        cv.put(COLUMN_USED_NAME,profile.used_name);
        cv.put(COLUMN_USED_EMAIL,profile.used_email);

        long insert = db.insert(PASSWORDS_TABLE, null, cv);

        if(insert==-1){
            return true;
        }else{
            return false;
        }

    }
}

此代码的问题是,它只是没有在data\data\com.example.passwordmanager中创建数据库或数据库的文件夹。我试着输出调用构造函数时它将到达的方法,我意识到它永远不会到达oncreate方法。
databasehelper类只被调用一次,因此:

package com.example.passwordmanager;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.hardware.input.InputManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class CreatingNewEntryActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_creating_new_entry);

        DatabaseHelper dh = new DatabaseHelper(CreatingNewEntryActivity.this);
}

这是代码的问题吗?我必须在manifest.xml中授予权限吗?我把模拟器搞砸了吗?应用程序启动正常,只是不能创建数据库。

j2qf4p5b

j2qf4p5b1#

你需要打电话 getWritableDatabase() 或者 getReadableDatabase() 在数据库助手上,使框架创建数据库文件和触发器 onCreate() . 仅仅调用构造函数是不够的。
另请参阅:sqliteopenhelper oncreate()/onupgrade()何时运行?

相关问题