简体   繁体   English

SQLite 文件打开器 Android Studio

[英]SQLite file opener Android Studio

I have just started an Android studio project, I have written some code to create a table within SQLite.我刚刚开始了一个 Android Studio 项目,我编写了一些代码来在 SQLite 中创建一个表。

There is no easy UI for the database so I tried to install the plugin DB Navigator.数据库没有简单的用户界面,所以我尝试安装插件 DB Navigator。 This requires I link the navigator to my database but cant find where the file will be created.这需要我将导航器链接到我的数据库,但无法找到文件的创建位置。

A lot of answers seen to be go to /data/data/ but I can't find these folders in my directory.很多答案都显示在 /data/data/ 中,但我在我的目录中找不到这些文件夹。

Any idea of how to pin point my SQLite database?知道如何确定我的 SQLite 数据库吗? Any help is greatly appreciated.任何帮助是极大的赞赏。

SQLiteController.java SQLiteController.java

  package com.example.apexstrength;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import static java.sql.Types.NULL;

public  class SQLiteController extends SQLiteOpenHelper { //abstract to prevent creating/dropping tables

    private static final int DATABASE_VERSION=1;
    private static final String DATABASE_NAME="ApexStrength";
    private static final String TABLE_USER="user";

    private static final String KEY_userName="userName",KEY_password="password", KEY_firstName="firstName",KEY_surname="surname", KEY_EMAIL="email";

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

    public void addUser( String userName, String password, String firstName, String surname, String email)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(KEY_userName, userName);
        values.put(KEY_password, password);
        values.put(KEY_firstName, firstName);
        values.put(KEY_surname, surname);
        values.put(KEY_EMAIL, email);


        //inserting rows
        db.insert(TABLE_USER, null, values);
        db.close();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE IF NOT EXISTS user (username VARCHAR, password VARCHAR, firstName VARCHAR, surname VARCHAR, email VARCHAR)"); //Create table is not there.
//        db.execSQL("CREATE TABLE IF NOT EXISTS exercise (username VARCHAR, password VARCHAR, firstName VARCHAR, surname VARCHAR, email VARCHAR)");
//        db.execSQL("CREATE TABLE IF NOT EXISTS workout (username VARCHAR, password VARCHAR, firstName VARCHAR, surname VARCHAR, email VARCHAR)");
//        db.execSQL("CREATE TABLE IF NOT EXISTS sets (username VARCHAR, password VARCHAR, firstName VARCHAR, surname VARCHAR, email VARCHAR)");
    }

    public boolean logIn(String username, String password) {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor c = db.rawQuery("select * from user where username=? and password=?", new String[]{username,password});
        if(c.getCount()>0)
        {
            return true;
        }
        return false;
    }

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

    }
}

signUp.java注册.java

package com.example.apexstrength;

import androidx.appcompat.app.AppCompatActivity;

import android.app.DownloadManager;
import android.content.Intent;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class signUp extends AppCompatActivity {
    private SQLiteController db;

    public void register(View view) {
        EditText user = (EditText) findViewById(R.id.passwordReg);
        EditText pass = (EditText) findViewById(R.id.userReg);
        EditText first = (EditText) findViewById(R.id.firstNameReg);
        EditText sur = (EditText) findViewById(R.id.surnameReg);
        EditText email = (EditText) findViewById(R.id.emailReg);

        String username = user.getText().toString();
        String password = pass.getText().toString();
        String firstname = first.getText().toString();
        String surname = sur.getText().toString();
        String emailAddress = email.getText().toString();

        if (!username.isEmpty() && !password.isEmpty() && !firstname.isEmpty() && !surname.isEmpty() && !emailAddress.isEmpty()) {
            db.addUser(username, password, firstname, surname, emailAddress);
            Toast.makeText(getApplicationContext(), emailAddress+ " has been registered!", Toast.LENGTH_LONG).show();
            Intent i = new Intent(getApplicationContext(), MainActivity.class);
            startActivity(i);
        } else {
            Toast.makeText(getApplicationContext(), "Please enter your details", Toast.LENGTH_LONG).show();
        }
    }

    public boolean onCreateOptionsMenu(Menu menu)                            //Creating options menu
    {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.main_menu, menu);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);

        switch (item.getItemId()) {
            //Selected items, log selected
            case R.id.home:
                Log.i("Menu item selected", "Home");
                Intent intent = new Intent(this, MainActivity.class);
                startActivity(intent);
                return true;
            case R.id.settings:
                Log.i("Menu item selected", "Settings");
                return true;
            case R.id.help:
                Log.i("Menu item selected", "Help");
                return true;
            case R.id.weightConversion:
                Log.i("Menu item selected", "Weight Conversion");
                intent = new Intent(this, weightConversion.class);
                startActivity(intent);
                return true;
            default:
                return false;

        }
    }



    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up);
        db= new SQLiteController(getApplicationContext());
    }}

In Android you can use Device Explorer (bottom right by default or via View / Tool Windows / Device Explorer)在 Android 中,您可以使用设备资源管理器(默认右下角或通过视图/工具窗口/设备资源管理器)

在此处输入图片说明

and then something along the lines of :-然后是:-

在此处输入图片说明

You can then right click the database and use Save As or Copy Path然后您可以右键单击数据库并使用另存为复制路径

  • Note if there is a -wal file and it is not empty then you should copy the -wal and - shm files along with the database file.请注意,如果有 -wal 文件并且它不为空,那么您应该将 -wal 和 -shm 文件与数据库文件一起复制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM