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

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

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

  1. import android.os.Bundle;
  2. import android.support.v7.app.AppCompatActivity;
  3. import android.support.v7.widget.Toolbar;
  4. import android.view.View;
  5. import android.widget.Button;
  6. import android.widget.EditText;
  7. import android.content.Context;
  8. import android.content.ContentValues;
  9. import android.content.Intent;
  10. import android.database.Cursor;
  11. import android.database.sqlite.SQLiteDatabase;
  12. import android.widget.Toast;
  13. public class Facultyreg extends AppCompatActivity implements View.OnClickListener {
  14. EditText name, address, qualification, salary, username, password, repassword;
  15. Button submit;
  16. SQLiteDatabase db;
  17. ContentValues cv;
  18. Cursor c;
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_facultyreg);
  23. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  24. setSupportActionBar(toolbar);
  25. name = (EditText) findViewById(R.id.sname);
  26. address = (EditText) findViewById(R.id.address);
  27. qualification = (EditText) findViewById(R.id.qualification);
  28. salary = (EditText) findViewById(R.id.salary);
  29. username = (EditText) findViewById(R.id.username);
  30. password = (EditText) findViewById(R.id.password);
  31. repassword = (EditText) findViewById(R.id.repassword);
  32. submit= (Button) findViewById(R.id.add);
  33. submit.setOnClickListener(this);
  34. try {
  35. db=openOrCreateDatabase("CMS",MODE_PRIVATE,null);
  36. db.execSQL("create table IF NOT EXISTS FACULTY(FID INTEGER PRIMARY KEY AUTOINCREMENT,"
  37. + "NAME varchar(50) NOT NULL,"
  38. + "ADDRESS varchar(50),"
  39. + "QUALIFICATION varchar(50) ,"
  40. + "SALARY varchar(50) NOT NULL,"
  41. + "USERNAME varchar(50) ,"
  42. + "PASSWORD varchar(100))");
  43. }
  44. catch (Exception e){
  45. Toast toast = Toast.makeText(Facultyreg.this, "DB not created.",Toast.LENGTH_SHORT);
  46. toast.show();
  47. }
  48. }
  49. @Override
  50. public void onClick(View v) {
  51. if (v.getId() == R.id.add) {
  52. onRegister();
  53. }
  54. }
  55. public void onRegister(){
  56. String fname= name.getText().toString();
  57. String add= address.getText().toString();
  58. String qual= qualification.getText().toString();
  59. String sal = salary.getText().toString();
  60. String uname = username.getText().toString();
  61. String pass= password.getText().toString();
  62. String repass= (repassword.getText().toString());
  63. if(fname.isEmpty()){
  64. Toast toast = Toast.makeText(Facultyreg.this, "Please Enter Name.",Toast.LENGTH_SHORT);
  65. toast.show();
  66. name.setFocusable(true);
  67. name.requestFocus();
  68. }
  69. else if(add.isEmpty()){
  70. Toast toast = Toast.makeText(Facultyreg.this, "Please Enter Address.",Toast.LENGTH_SHORT);
  71. toast.show();
  72. address.setFocusable(true);
  73. address.requestFocus();
  74. }
  75. else if(sal.isEmpty()){
  76. Toast toast = Toast.makeText(Facultyreg.this, "Please Enter Salary.",Toast.LENGTH_SHORT);
  77. toast.show();
  78. salary.setFocusable(true);
  79. salary.requestFocus();
  80. }
  81. else if(qual.isEmpty()){
  82. Toast toast = Toast.makeText(Facultyreg.this, "Please Enter qualification.",Toast.LENGTH_SHORT);
  83. toast.show();
  84. qualification.setFocusable(true);
  85. qualification.requestFocus();
  86. }
  87. else if(pass.isEmpty()){
  88. Toast toast = Toast.makeText(Facultyreg.this, "Please Enter Password.",Toast.LENGTH_SHORT);
  89. toast.show();
  90. password.setFocusable(true);
  91. password.requestFocus();
  92. }
  93. else if(repass.isEmpty()){
  94. Toast toast = Toast.makeText(Facultyreg.this, "Please Re-enter Password.",Toast.LENGTH_SHORT);
  95. toast.show();
  96. repassword.setFocusable(true);
  97. repassword.requestFocus();
  98. }
  99. else if(!pass.equals(repass)){
  100. Toast toast = Toast.makeText(Facultyreg.this, "Passwords do not match. Try again!",Toast.LENGTH_SHORT);
  101. toast.show();
  102. repassword.setFocusable(true);
  103. repassword.requestFocus();
  104. }
  105. else if(uname.isEmpty()){
  106. Toast toast = Toast.makeText(Facultyreg.this, "Please Enter Your Username",Toast.LENGTH_SHORT);
  107. toast.show();
  108. username.setFocusable(true);
  109. username.requestFocus();
  110. }
  111. else
  112. {
  113. cv=new ContentValues();
  114. cv.put("NAME",fname);
  115. cv.put("ADDRESS",add);
  116. cv.put("QUALIFICATION",qual);
  117. cv.put("SALARY",sal);
  118. cv.put("USERNAME",uname);
  119. cv.put("PASSWORD",pass);
  120. db.insert("FACULTY", null, cv);
  121. Toast toast = Toast.makeText(Facultyreg.this, "Registered.",Toast.LENGTH_SHORT);
  122. toast.show();
  123. Intent in=new Intent();
  124. setResult(175,in);
  125. finish();
  126. }
  127. }

}

lc8prwob

lc8prwob1#

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

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

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

zd287kbt

zd287kbt2#

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

  1. /****************use for create folder in device*****************/
  2. File sd = Environment.getExternalStorageDirectory();
  3. File data = Environment.getDataDirectory();
  4. String path =Environment.getExternalStorageDirectory() +File.separator + "CMS";
  5. File folder = new File(path);
  6. boolean success = true;
  7. if (!folder.exists()) {
  8. success = folder.mkdir();
  9. }
  10. if (success) {
  11. // Do something on success
  12. } else {
  13. // Do something else on failure
  14. }
  15. /****************use for create folder in device*****************/
  16. /****************use for copy data from database*****************/
  17. if (sd.canWrite()) {
  18. String currentDBPath = "/data/data/"+ getPackageName()+"/databases/CMS";// db path
  19. String backupDBPath = "dbname.db";
  20. File currentDB = new File(currentDBPath);
  21. File backupDB = new File(path, backupDBPath);
  22. //to get the run time db file
  23. if (currentDB.exists()) {
  24. FileChannel src = null;
  25. try {
  26. src = new FileInputStream(currentDB).getChannel();
  27. } catch (FileNotFoundException e1) {
  28. e1.printStackTrace();
  29. }
  30. FileChannel dst = null;
  31. try {
  32. dst = new FileOutputStream(backupDB).getChannel();
  33. } catch (FileNotFoundException e1) {
  34. e1.printStackTrace();
  35. }
  36. try {
  37. dst.transferFrom(src, 0, src.size());
  38. src.close();
  39. dst.close();
  40. } catch (IOException e1) {
  41. e1.printStackTrace();
  42. }
  43. }
  44. }
  45. /****************use for copy data from database*****************/

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

展开查看全部

相关问题