我无法在android中的sqlite数据库中插入记录

时间:2016-04-09 18:44:35

标签: android android-sqlite

我制作了1个pojo类,1个DbHelper类和Insert Activity.But仍然无法在数据库中插入记录。

DoctorPojo.java(Pojo类)

public class DoctorPojo {
String name,mobile,add,gender,city,cas,disease;
Date dob,date;
int id;

public DoctorPojo()
{

}
public DoctorPojo( String name,Date dob,String gender, String add,String city,
                   String cas,String mobile, Date date,String disease)
{
    this.name = name;
    this.dob = dob;
    this.gender = gender;
    this.add = add;
    this.city = city;
    this.cas = cas;
    this.mobile = mobile;
    this.date = date;
    this.disease = disease;
}

public DoctorPojo(int id,String name,Date dob,String gender, String add,String city,
                  String cas,String mobile, Date date,String disease)
{
    this.id = id;
    this.name = name;
    this.dob = dob;
    this.gender = gender;
    this.add = add;
    this.city = city;
    this.cas = cas;
    this.mobile = mobile;
    this.date = date;
    this.disease = disease;
}

public String getName() {
    return name;
}

public String getMobile() {
    return mobile;
}

public String getAdd() {
    return add;
}

public String getGender() {
    return gender;
}

public String getCity() {
    return city;
}

public String getCas() {
    return cas;
}

public String getDisease() {
    return disease;
}

public Date getDob() {
    return dob;
}

public Date getDate() {
    return date;
}

public int getId() {
    return id;
}

}

DbHelper.java

public class DbHelper extends SQLiteOpenHelper {

public static final String DB_NAME="hms";
public static final String TB_NAME="doctor";
public static final String _ID="_id";
public static final String NAME="NAME";
public static final String DOB="DOB";
public static final String GENDER="GENDER";
public static final String ADDRESS="ADDRESS";
public static final String CITY="CITY";
public static final String CASE="CASE";
public static final String MOBILE="MOBILE";
public static final String DATE="DATE";
public static final String DISEASE="DISEASE";
public static final int VERSION=1;



public DbHelper(Context context)
{
    super(context,TB_NAME,null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
    //create table doctor(id );
    String query="CREATE TABLE "+TB_NAME+"("+_ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"
            +NAME+" TEXT,"
            +DOB+"DATE,"
            +GENDER+"TEXT,"
            +ADDRESS+"TEXT,"
            +CITY+"TEXT,"
            +CASE+"TEXT,"
            +MOBILE+"TEXT,"
            +DATE+"DATE,"
            +DISEASE+"TEXT)";
    db.execSQL(query);
}


@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}
public void Insert(DoctorPojo dp)
{
    SQLiteDatabase db=getWritableDatabase();

    ContentValues cv=new ContentValues();
    cv.put(NAME,dp.getName());
    String a=String.valueOf(dp.getDob());
    cv.put(DOB,a);
    cv.put(GENDER,dp.getGender());
    cv.put(ADDRESS,dp.getAdd());
    cv.put(CITY,dp.getCity());
    cv.put(CASE,dp.getCas());
    cv.put(MOBILE,dp.getMobile());
    String b=String.valueOf(dp.getDob());
    cv.put(DOB,b);
    cv.put(DISEASE,dp.getDisease());
    db.insert(TB_NAME,null,cv);
}

InsertActivity.java

public class InsertActivity extends AppCompatActivity {
Button ins;
EditText ename, edob, eaddress, ecity, ecase, emobile, edate, edisease;
RadioGroup r1;
RadioButton rd;

final Calendar myCalendar = Calendar.getInstance();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_insert);

    ename = (EditText) findViewById(R.id.ename);
    edob = (EditText) findViewById(R.id.edob);
    eaddress = (EditText) findViewById(R.id.eaddres);
    ecity = (EditText) findViewById(R.id.ecity);
    ecase = (EditText) findViewById(R.id.ecase);
    emobile = (EditText) findViewById(R.id.emobile);
    edate = (EditText) findViewById(R.id.edate);
    edisease = (EditText) findViewById(R.id.edisease);
    ins=(Button)findViewById(R.id.button);
    r1= (RadioGroup) findViewById(R.id.RadioGroup);



    final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {
            // TODO Auto-generated method stub
            myCalendar.set(Calendar.YEAR, year);
            myCalendar.set(Calendar.MONTH, monthOfYear);
            myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
            updateLabel();
        }
    };

    final DatePickerDialog.OnDateSetListener date1 = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {
            // TODO Auto-generated method stub
            myCalendar.set(Calendar.YEAR, year);
            myCalendar.set(Calendar.MONTH, monthOfYear);
            myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
            updateLabel1();
        }
    };

    edob.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new DatePickerDialog(InsertActivity.this, date, myCalendar
                    .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                    myCalendar.get(Calendar.DAY_OF_MONTH)).show();
        }
    });
    edate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new DatePickerDialog(InsertActivity.this, date1, myCalendar
                    .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                    myCalendar.get(Calendar.DAY_OF_MONTH)).show();
        }
    });
    ins.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int selected=r1.getCheckedRadioButtonId();
            rd=(RadioButton)findViewById(selected);
            if(selected == 0)
            {
                String radioButtonText = rd.getText().toString();
            }
            else
            {
                String radioButtonText = rd.getText().toString();
            }
            DoctorPojo dp=new DoctorPojo();
            DbHelper db=new DbHelper(InsertActivity.this);
            db.Insert(dp);
            Toast.makeText(InsertActivity.this,"Record is Inserted...!!!",Toast.LENGTH_LONG).show();
        }
    });
}

private void updateLabel() {

    String myFormat = "MM/dd/yy"; //In which you need put here
    SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

    edob.setText(sdf.format(myCalendar.getTime()));

}
private void updateLabel1() {

    String myFormat = "MM/dd/yy"; //In which you need put here
    SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
    edate.setText(sdf.format(myCalendar.getTime()));
}

} 请帮我。 我试了很多找错误。但它没有向我显示错误。并且它没有将代码插入数据库。

0 个答案:

没有答案