-1

I am making an app with flutter and storing the data in SQL database using sqflite. How can I store the Date and time in which the form submitted or the other information passed
image of the form is attached Currently, I am storing Date as a string (TEXT), but while sorting data entered between dates as it is a string it is not working. Is there a method by which I can store Date and Time in DateTime using sqflite? (If yes please describe the method). If I can parse the dates stored in the string to DateTime somehow while retrieving the information that can help as well but I don't know how can I do it?

Edit:

I am using DateTime format MM-DD-YYYY. I want to store the "date" in the database so that when I need to retrieve the data I can use the 'between' SQL query method to retrieve data, for example:

Future getDataJan() async{
final db = await database;
sumJan=await db.rawQuery(
'SELECT SUM(AMOUNT) FROM EXPENSES WHERE DATE(DATETIME) >= ? AND DATE(DATETIME) <= ?',
    [2021-01-01, 2021-01-31] ).then(Sqflite.firstIntValue);
print(sumJan);
finalJan=sumJan.toString();

}

I am using the above function for gathering all the expenses done(modified after the answer from "xahid_rocks") in a month and then summing up the expenses and returning that in a display form. This is one of the use of what I want to do.

Edit 2:

Now when I am calling this it is showing null. This is my code for making a database.

await database.execute(
      "CREATE TABLE $TABLE_EXPENSES ("
      "$COLUMN_ID INTEGER PRIMARY KEY,"
      "$COLUMN_NAME TEXT,"
      "$COLUMN_AMOUNT INTEGER,"
      "$COLUMN_UNNECESSARYEXPENSES INTEGER,"
      "$COLUMN_CATEGORY TEXT,"
      "$COLUMN_DATETIME TEXT"
      ")",

Thanks for all your answers and replies.

Sanchay Kasturey
  • 161
  • 1
  • 17

1 Answers1

0

You can add date function before the key

sumJan= db.rawQuery('SELECT SUM(AMOUNT) FROM EXPENSES WHERE DATE(DATETIME) >= ? AND DATE(DATETIME) <= ?', [2021-01-01, 2021-01-31] ).then(Sqflite.firstIntValue);
xahid_rocks
  • 632
  • 8
  • 24