我写了一个注册代码但是没有在android设备管理器中创建数据库。我该怎么办。在DDMS中没有创建数据文件 - > file explorer - > data.Code正在运行但无法查看数据库。我在手机上运行app而不是模拟器
码
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();
}
}
}
答案 0 :(得分:1)
DDMS文件资源管理器只允许在模拟器和root设备上访问data->data
。
您可以在PC上查看和浏览SQLite
数据库。从here
现在,要从非root 设备获取SQLite
db文件,请打开命令提示符并通过添加正确的<运行以下命令strong>包名称和 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文件将是您从中打开命令提示符的位置。只需在之前下载的数据库浏览器中打开它即可。
答案 1 :(得分:0)
首先,您可以在emulater(rooted device)的帮助下在DDMS中查看数据库。
如果您想查看具有真实设备的数据库,那么您可以复制该数据库并保存到一个文件夹中。之后,您可以在&#34;数据库浏览器软件&#34;的帮助下查看数据库。或其他。
将副本数据库放入一个文件夹即可使用。
/****************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文件夹中找到数据库。
我希望这会对你有所帮助。谢谢
抱歉我的英文。