如何在androidstudio中创建数据库因为我写的代码不是在ddms中创建数据库

dy2hfwbg  于 2021-07-13  发布在  Java
关注(0)|答案(2)|浏览(401)

我写了一个注册代码,但数据库不是在android设备管理器中创建的。我该怎么办。在ddms-->file explorer-->data中未创建任何数据文件。代码正在运行,但无法看到数据库。我在手机上运行应用程序,而不是模拟器
代码

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.content.Context;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;

public class Facultyreg extends AppCompatActivity implements    View.OnClickListener {
EditText name, address, qualification, salary, username, password, repassword;
Button submit;
SQLiteDatabase db;
ContentValues cv;
Cursor c;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_facultyreg);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    name = (EditText) findViewById(R.id.sname);
    address = (EditText) findViewById(R.id.address);
    qualification = (EditText) findViewById(R.id.qualification);
    salary = (EditText) findViewById(R.id.salary);
    username = (EditText) findViewById(R.id.username);
    password = (EditText) findViewById(R.id.password);
    repassword = (EditText) findViewById(R.id.repassword);

    submit= (Button) findViewById(R.id.add);

    submit.setOnClickListener(this);
    try {
        db=openOrCreateDatabase("CMS",MODE_PRIVATE,null);

        db.execSQL("create table IF NOT EXISTS FACULTY(FID INTEGER PRIMARY KEY AUTOINCREMENT,"
                + "NAME varchar(50) NOT NULL,"
                + "ADDRESS varchar(50),"
                + "QUALIFICATION varchar(50) ,"
                + "SALARY varchar(50) NOT NULL,"
                + "USERNAME varchar(50) ,"
                + "PASSWORD varchar(100))");
    }
      catch (Exception e){
          Toast toast = Toast.makeText(Facultyreg.this, "DB not         created.",Toast.LENGTH_SHORT);
          toast.show();

      }

}

@Override
public void onClick(View v) {
    if (v.getId() == R.id.add) {
        onRegister();

    }
}

public void onRegister(){

    String fname= name.getText().toString();
    String add= address.getText().toString();
    String qual= qualification.getText().toString();
    String sal = salary.getText().toString();
    String uname = username.getText().toString();
    String pass= password.getText().toString();
    String repass= (repassword.getText().toString());

    if(fname.isEmpty()){
        Toast toast = Toast.makeText(Facultyreg.this, "Please Enter Name.",Toast.LENGTH_SHORT);
        toast.show();
        name.setFocusable(true);
        name.requestFocus();
    }
    else if(add.isEmpty()){
        Toast toast = Toast.makeText(Facultyreg.this, "Please Enter Address.",Toast.LENGTH_SHORT);
        toast.show();
        address.setFocusable(true);
        address.requestFocus();
    }

    else if(sal.isEmpty()){
        Toast toast = Toast.makeText(Facultyreg.this, "Please Enter Salary.",Toast.LENGTH_SHORT);
        toast.show();
        salary.setFocusable(true);
        salary.requestFocus();
    }

    else if(qual.isEmpty()){
        Toast toast = Toast.makeText(Facultyreg.this, "Please Enter qualification.",Toast.LENGTH_SHORT);
        toast.show();
        qualification.setFocusable(true);
        qualification.requestFocus();
    }
    else if(pass.isEmpty()){
        Toast toast = Toast.makeText(Facultyreg.this, "Please Enter Password.",Toast.LENGTH_SHORT);
        toast.show();
        password.setFocusable(true);
        password.requestFocus();
    }
    else if(repass.isEmpty()){
        Toast toast = Toast.makeText(Facultyreg.this, "Please Re-enter Password.",Toast.LENGTH_SHORT);
        toast.show();
        repassword.setFocusable(true);
        repassword.requestFocus();
    }
    else if(!pass.equals(repass)){
        Toast toast = Toast.makeText(Facultyreg.this, "Passwords do not match. Try again!",Toast.LENGTH_SHORT);
        toast.show();
        repassword.setFocusable(true);
        repassword.requestFocus();
    }

    else if(uname.isEmpty()){
        Toast toast = Toast.makeText(Facultyreg.this, "Please Enter Your Username",Toast.LENGTH_SHORT);
        toast.show();
        username.setFocusable(true);
        username.requestFocus();
    }
    else
    {
        cv=new ContentValues();
        cv.put("NAME",fname);
        cv.put("ADDRESS",add);
        cv.put("QUALIFICATION",qual);
        cv.put("SALARY",sal);
        cv.put("USERNAME",uname);
        cv.put("PASSWORD",pass);
        db.insert("FACULTY", null, cv);

        Toast toast = Toast.makeText(Facultyreg.this, "Registered.",Toast.LENGTH_SHORT);
        toast.show();

        Intent in=new Intent();
        setResult(175,in);
        finish();

    }
}

}

lc8prwob

lc8prwob1#

ddms文件资源管理器允许访问 data->data 仅在模拟器和根设备上。
您可以查看和浏览 SQLite 从这里下载数据库浏览器
现在,为了得到 SQLite 从非根设备的db文件,打开命令提示符并通过添加正确的包名和db文件名来运行以下命令

adb shell "run-as com.package.name cp /data/data/com.package.name/databases/dbname.sqlite /sdcard/dbname.sqlite"
adb pull /sdcard/dbname.sqlite

这个 SQLite db文件将是打开命令提示符的位置。只需在之前下载的db浏览器中打开它。

zd287kbt

zd287kbt2#

首先,在模拟器(根设备)的帮助下,您可以在ddms中看到数据库。
如果你想用真实的设备查看数据库,那么你可以复制数据库并保存到一个文件夹中,然后你可以借助“数据库浏览器软件”或其他工具查看数据库。
要将数据库复制到一个文件夹中,可以使用此选项。

/****************use for create folder in device*****************/
        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();

        String path =Environment.getExternalStorageDirectory() +File.separator + "CMS";

        File folder = new File(path);
        boolean success = true;
        if (!folder.exists()) {
            success = folder.mkdir();
        }
        if (success) {
            // Do something on success
        } else {
            // Do something else on failure
        }
       /****************use for create folder in device*****************/

        /****************use for copy data from database*****************/
        if (sd.canWrite()) {

            String currentDBPath = "/data/data/"+ getPackageName()+"/databases/CMS";// db path

            String backupDBPath = "dbname.db";
            File currentDB = new File(currentDBPath);
            File backupDB = new File(path, backupDBPath);
            //to get the run time db file
            if (currentDB.exists()) {
                FileChannel src = null;
                try {
                    src = new FileInputStream(currentDB).getChannel();
                } catch (FileNotFoundException e1) {
                    e1.printStackTrace();
                }
                FileChannel dst = null;
                try {
                    dst = new FileOutputStream(backupDB).getChannel();
                } catch (FileNotFoundException e1) {
                    e1.printStackTrace();
                }
                try {
                    dst.transferFrom(src, 0, src.size());
                    src.close();
                    dst.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }

            }

          }
        /****************use for copy data from database*****************/

你可以在你的真实设备的cms文件夹中找到数据库。
我希望这对你有帮助。谢谢
对不起我的英语。

相关问题