lundi 28 mars 2016

How to create database in android studio. as the code i wrote is not creating database in ddms

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();

    }
}

}

Aucun commentaire:

Enregistrer un commentaire