0

i want to insert date in Sqlite database i'm stuck at this point. I have taken Date datatype in my pojo class. now i want to insert that thing in my Sqlite Databse using contentvalue so what should i do. it is like this.

 SQLiteDatabase db = getWritableDatabase();
    ContentValues cv = new ContentValues();
    String a = String.valueOf(dp.getDob());
    cv.put(DOB, a);

in pojo class

Date dob;

public DoctorPojo( String name,Date dob,String gender, String adder,String city,
                   String cas,String mobile, Date date,String disease)
{
    this.name = name;
    this.dob = dob;
    this.gender = gender;
    this.adder = adder;
    this.city = city;
    this.cas = cas;
    this.mobile = mobile;
    this.date = date;
    this.disease = disease;
}
2
  • Possible duplicate of tips.androidhive.info/2013/10/… Commented Apr 12, 2016 at 18:30
  • but now what should i have to take in my create statement's date? Commented Apr 12, 2016 at 18:32

1 Answer 1

1

According to the Sqlite specficication https://www.sqlite.org/lang_datefunc.html , You have to store Date as String with format YYYY-MM-DD.

It's easy to do that with SimpleDateFormatter.

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = formatter.format(dp.getDob());
cv.put(DOB, formattedDate );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.