Reputation: 29
I wrote a code for registration but database is not created in android device manager. what should i do. No data file is been created in DDMS--> file explorer-->data.Code is running but cannot see database. i'm running app on phone and not emulator
code
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();
}
}
}
Upvotes: 2
Views: 769
Reputation: 1457
DDMS File explorer allows access to data->data
only on emulators and rooted devices.
You can view and browse an SQLite
database on PC. Download the db browser from here
Now, to get the SQLite
db file from a non-rooted device, open a command prompt and run the following commands by adding your proper package name and db file name
adb shell "run-as com.package.name cp /data/data/com.package.name/databases/dbname.sqlite /sdcard/dbname.sqlite"
adb pull /sdcard/dbname.sqlite
The SQLite
db file will be where you opened the command prompt from. Just open it in the db browser you downloaded earlier.
Upvotes: 1
Reputation: 687
Firstly , You can see database in DDMS with the help of emulater (rooted device).
If you want to see database with real device, then you can copy that database and save into one folder.After that you can see database with the help of "DB Browser software" or other.
for copy database into one folder you can use this.
/****************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*****************/
You find database in CMS folder in your real device.
I hope this will help you. thanks
Sorry for my english.
Upvotes: 0