0
Answer

Error while inserting date and time in sqlite database

Nandhini Jayachandran

Nandhini Jayachandran

10y
739
1
After getting time and date from date picker and time picker .I want to insert date and time separately in sqlite db .
<pre>final String name1=name.getText().toString();
final String mobile_number1=mobile_number.getText().toString();
final String home_number1=home_number.getText().toString();
final String company_name1=company_name.getText().toString();
final String jobtitle1=jobtitle.getText().toString();
final String emailid1=emailid.getText().toString();
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
String dateInString = setdate.toString();
String timeInString=settime.toString();
Date date = null;
Time time=null;

try {

//Date date;
try {
date = formatter.parse(dateInString);
time=(Time) formatter.parse(timeInString);
} catch (java.text.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(date);
System.out.println(formatter.format(date));

} catch (ParseException e) {
e.printStackTrace();
}
db.addContact(new Contact_details(name1,mobile_number1,home_number1,company_name1,jobtitle1,emailid1,date,time));
Toast.makeText(getApplicationContext(), ""+date+""+time, Toast.LENGTH_LONG).show();</pre>
code to create table in sqlite

<pre lang="cs">String CREATE_CONTACTS_TABLE = &quot;CREATE TABLE &quot; + TABLE_CONTACTS + &quot;(&quot;
+ KEY_ID + &quot; INTEGER PRIMARY KEY,&quot; + KEY_NAME + &quot; TEXT,&quot;
+ KEY_Mobile_no + &quot; TEXT&quot; + KEY_Home_no+&quot; TEXT&quot;+ KEY_Emailid +&quot; TEXT&quot;+ KEY_Job_title +&quot; TEXT&quot;+ KEY_Company_name +&quot; TEXT&quot;+ KEY_Date +&quot; DATE&quot;+ KEY_Time +&quot; TIME&quot;+&quot;)&quot;;
db.execSQL(CREATE_CONTACTS_TABLE);</pre>
code to insert into sqlite
<pre>void addContact(Contact_details contact) {
SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.getName()); // Contact Name
values.put(KEY_Mobile_no, contact.getMobile_number()); // Contact Mobile_number
values.put(KEY_Home_no, contact.getHome_number());
values.put(KEY_Emailid, contact.getEmail_id());
values.put(KEY_Job_title, contact.getJob_title());
values.put(KEY_Company_name,contact.getCompany_title());
values.put(KEY_Date, contact.getSet_date().toString()) ;
values.put(KEY_Time, contact.getSet_time().toString());


// Inserting Row
db.insert(TABLE_CONTACTS, null, values);
//2nd argument is String containing nullColumnHack
db.close(); // Closing database connection
}</pre>
but i am getting unfortunately app stopped error.I dnt know where i have made error ?