Vaishak Babu
Vaishak Babu

Reputation: 3

how to store date from datepicker to sqlite?

My code is:

 taskDB.insertDate(selectedDate,selectedId);

SQlite DB:

String sql = "CREATE TABLE IF NOT EXISTS " +TABLE_NAME+ "(" +KEY_ID+ " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +KEY_TASKNAME+ " TEXT, " +KEY_STATUS+ " INTEGER," +KEY_FAV+ " INTEGER," +KEY_DATE+ " TEXT"+")";
    db.execSQL(sql);

Insert query:

public void insertDate(String date, int id){
    String sql = "UPDATE task SET date = "+date+" WHERE id = " +id;
    SQLiteDatabase db = this.getWritableDatabase();
    Log.d(TAG,"id is:" +id+ ", date is: "+date);// log is below
    db.execSQL(sql);
}

Log:

05-26 10:23:14.538 15298-15298/? D/TaskDB: id is:2, date is: 17-05-2017

In log of insert query i get the correct date that i selected from the datepicker but in the table it is in different format.This is the screenshot of sqlite table after insertion of date

Upvotes: 0

Views: 793

Answers (2)

Pavneet_Singh
Pavneet_Singh

Reputation: 37404

As commented , you need to put single quotes around date to represent string, like

"UPDATE task SET date = '"+date+"' WHERE id = " +id;
//                      ^        ^

From docs

A string constant is formed by enclosing the string in single quotes ('). A single quote within the string can be encoded by putting two single quotes in a row - as in Pascal. C-style escapes using the backslash character are not supported because they are not standard SQL.

Upvotes: 1

Dishonered
Dishonered

Reputation: 8841

Change the query like this and try again

String sql = "UPDATE task SET date = '"+date+"' WHERE id = " +id;

I have changed placed the date in single quotes , whenever you are adding a string value into a query , you need to place them in single quotes.

Upvotes: 0

Related Questions