How to get data month wise sqflite in Flutter?
You can operate with a date with format like yyyy-MM-dd HH:mm:SS
and create requests to database for search entities between month.
Date format example:
DateFormat('yyyy-MM-dd HH:mm:SS').format(date)
Query example:
return database.query(
tableName,
where: 'date >= ? and date <= ?',
whereArgs: [from, to],
)
.then((data) => data.map(_fromMap).toList());
Variables in example:
from
- first day of month (2020-10-01);to
- last day of month (2020-10-31);?
inwhere
- takes values fromwhereArgs
(first?
-from
, second -to
);
Note: in comments describes that you should use SQFLite tooling for creating requests and do not use raw queries (for performance reasons).
Select example
final result = await database.rawQuery(
'select * sum(COLUMN_AMOUNT) from TABLE_EXPENSES where COLUMN_DATETIME >= ? and COLUMN_DATETIME <= ?',
[from, to],
).then(Sqflite.firstIntValue);
Sanchay Kasturey
Updated on December 27, 2022Comments
-
Sanchay Kasturey over 1 year
Image for how does data look I am making an expense tracker app using flutter, I want to show the sum of expenses done by the user every month for example:
Month Amount Spended January 2000 February 1600 Database columns: "CREATE TABLE $TABLE_EXPENSES (" "$COLUMN_ID INTEGER PRIMARY KEY," "$COLUMN_NAME TEXT," "$COLUMN_AMOUNT TEXT," "$COLUMN_UNNECESSARYEXPENSES INTEGER," "$COLUMN_CATEGORY TEXT," "$COLUMN_DATETIME TEXT" ")",
I am using sqflite to create a database and I am storing data as text. I want to retrieve expenses of every day in a month and then sum up the expenses of every day and show it in ListTile
Edit:
Query: 'SELECT * SUM(COLUMN_AMOUNT) FROM TABLE_EXPENSES WHERE COLUMN_DATETIME >= ? AND COLUMN_DATETIME <= ?'
This is the error that I am getting (for query):
E/SQLiteLog(10318): (1) near "SELECT": syntax error E/flutter (10318): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: DatabaseException(near "SELECT": syntax error (code 1 SQLITE_ERROR): , while compiling: SELECT * FROM expenses WHERE SELECT * SUM(COLUMN_AMOUNT) FROM TABLE_EXPENSES WHERE COLUMN_DATETIME >= 2019 AND COLUMN_DATETIME <= 1989) sql 'SELECT * FROM expenses WHERE SELECT * SUM(COLUMN_AMOUNT) FROM TABLE_EXPENSES WHERE COLUMN_DATETIME >= ? AND COLUMN_DATETIME <= ?' args [2019, 1989]}
Function which I am currently using:
Future getDataJan() async{ final db = await database; sumJan= db.rawQuery( 'SELECT SUM(AMOUNT) FROM EXPENSES WHERE DATETIME >= ? AND DATETIME <= ?', [2021-01-01, 2021-01-31] ).then(Sqflite.firstIntValue); finalJan=sumJan.toString();
}
Image: errorImage
Thanks for your replies.
-
Ahx about 3 yearsWhere is the MCVE of the question? Please include it to the question. Also make sure to read How do I ask good question?
-
forpas about 3 yearsPost sample data of your table.
-
Sanchay Kasturey about 3 yearsI've added it hanks
-
-
Sanchay Kasturey about 3 yearscan i use group by month over here and if yes hw can I use it?
-
fartem about 3 yearsYou can try to use
groupBy
. Something likegroup by date
, -
Randal Schwartz about 3 yearsPlease don't interpolate into a where clause. Use placeholders. See bobby-tables.com for details.
-
fartem about 3 years@RandalSchwartz Thanks, I am complete the answer.
-
Sanchay Kasturey about 3 yearsI am using SQFlite only, thanks for your updated answer and reply I will try this one..
-
Sanchay Kasturey about 3 yearsFuture getDataJan() async{ final db = await database; sumJan= db.query(TABLE_EXPENSES, where: 'SELECT SUM(COLUMN_AMOUNT) FROM TABLE_EXPENSES WHERE COLUMN_DATETIME >= ? AND COLUMN_DATETIME <= ?', whereArgs:[2021-01-01, 2021-01-31] ); } can I use it like this as I need sum as well?
-
Sanchay Kasturey about 3 yearsyeah i am getting one ::[ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: DatabaseException(near "SELECT": syntax error (code 1 SQLITE_ERROR): , while compiling: SELECT * FROM expenses WHERE SELECT * SUM(COLUMN_AMOUNT) FROM TABLE_EXPENSES WHERE COLUMN_DATETIME >= 2019 AND COLUMN_DATETIME <= 1989) sql 'SELECT * FROM expenses WHERE SELECT * SUM(COLUMN_AMOUNT) FROM TABLE_EXPENSES WHERE COLUMN_DATETIME >= ? AND COLUMN_DATETIME <= ?' args [2019, 1989]}
-
fartem about 3 yearsPlease verify your SQL syntax.
-
Sanchay Kasturey about 3 yearsI don't know where am i wrong can you please help me??
-
Sanchay Kasturey about 3 yearsSELECT SUM(COLUMN_AMOUNT) FROM TABLE_EXPENSES WHERE COLUMN_DATETIME >= ? AND COLUMN_DATETIME <= ?', whereArgs:[2021-01-01, 2021-01-31] here it is
-
fartem about 3 yearsAnd you get error in SQL query or on the Flutter side?
-
Sanchay Kasturey about 3 yearsI am getting error on sql query can you please help me?
-
fartem about 3 yearsYes, can you add error message to the question?
-
Sanchay Kasturey about 3 yearsI have added the query as well as the error I am getting
-
Sanchay Kasturey about 3 yearsbut i want to store the sum of that amount in a variable, how can I do that?
-
fartem about 3 yearsYou can do this by call
count
function from sqflite and extract a value bySqflite.firstIntValue
method. -
Sanchay Kasturey about 3 yearsokay but then i would need to do the same for every month how would I differentiate it , I am not getting it please explain, currently I am making a function and then calling its variable :::::: 'Future getDataJan() async{ final db = await database; sumJan= db.query(TABLE_EXPENSES, where: 'SELECT * SUM(COLUMN_AMOUNT) FROM TABLE_EXPENSES WHERE COLUMN_DATETIME >= ? AND COLUMN_DATETIME <= ?', whereArgs:[2021-01-01, 2021-01-31] ); finalJan=sumJan.toString(); }'
-
fartem about 3 yearsCan you add your function to the question?
-
Sanchay Kasturey about 3 yearsI have added that
-
fartem about 3 yearsYou need to call raw query from sqflite, without
where
statement. -
Sanchay Kasturey about 3 yearscan you please provide an example to me? It would be really helpful
-
Sanchay Kasturey about 3 yearsI tried what you said but now I am getting an error the image for same has been posted in "errorImage"
-
fartem about 3 yearsYou need to extract value with
await
. Database get result asFuture
. -
Sanchay Kasturey about 3 yearsbut i am doing it, I have updated my function (in question) can you please check it and tell me ?
-
fartem about 3 yearsYou need to call function by this way:
sumJan = await db.rawQuery
. -
fartem about 3 yearsDelete this line
then(Sqflite.firstIntValue)
and add thisthen((result) => result['sum('COLUMN_AMOUNT')'])
; -
Sanchay Kasturey about 3 yearsNo it is showing an error when I type result, error "Undefined name 'result' ".
-
Sanchay Kasturey about 3 yearsI guess the problem is in my function it is mentioned under "Function i am using" I am trying to get data in sumJan=query* then I am parsing it to string finalJan and then I am calling finalJan in my main file where I need to display the sum, I guess somewhere here I am doing wrong I guess the way of assigning in sumJan is wrong please refer to the question for the code of the same.
-
fartem about 3 yearsYou can extract your database from the app's folder and test it with SQLite Browser.
-
Sanchay Kasturey about 3 yearsOkay i will try that