16

I create a TABLE with some columns and everything's ok until I tried to insert a new DATETIME column:

_onCreate(Database db, int version) async {
await db.
execute("CREATE TABLE $TABLE ($ID INTEGER PRIMARY KEY, $NAME TEXT, $COLOUR 
TEXT, $BREED TEXT, $BIRTH DATETIME)");
}

My Model Class to store is:

class Pet {
 int id;
 String name;
 DateTime birth;
 String breed;
 String colour;

 Pet({this.id, this.name, this.birth, this.colour, this.breed});
 }

In the controller, I tried to store a new instance of Pet, instantiating a new variable DateTime _date = new DateTime.now(); and saving all in

Pet newPet = Pet(
      id: null,
      name: _name,
      colour: _colour,
      breed: _breed,
      birth: _date
  );

But when I insert in the database I receive:

Unhandled Exception: Invalid argument: Instance of 'DateTime'

2
  • Why is $ prefexied for table names? Commented Mar 2, 2021 at 19:25
  • 1
    @AnoopThiruonam to avoid hard-coding the table name. Commented Feb 16, 2022 at 16:04

2 Answers 2

39

You are passing DateTime object directly.

Use class methods like:

https://api.dart.dev/stable/2.9.3/dart-core/DateTime/millisecondsSinceEpoch.html

or

https://api.dartlang.org/stable/2.4.0/dart-core/DateTime/toIso8601String.html

You need to pass String or int instead of an Object. The reason being, you must use supported SQLite data types to store in a SQLite table, find a list of supported data types here https://www.sqlite.org/datatype3.html

Also checkout:

https://pub.dev/packages/sqflite

DateTime is not a supported SQLite type. Personally I store them as int (millisSinceEpoch) or string (iso8601)

To retrieve your DateTime object again:

From Integer:

DateTime.fromMillisecondsSinceEpoch(yourValue);

From String:

DateTime.parse(yourValue);

or better way:

DateTime.tryParse(yourValue);
Sign up to request clarification or add additional context in comments.

2 Comments

If I store dates as int(millsSinceEpoch), can I retrieve based on dates? for example I want to get data based on dates, so select * from tablename ORDER BY date_int;
Yes @AthiraReddy , DateTime.fromMillisecondsSinceEpoch() constructor will crate new DateTime object from your integer, I edited my answer checkout.
0

Hi (my english is not good), i can solve this problem with sqlite3 package in my windows software (Flutter).

You should use text format for date:

db.execute('''
    CREATE TABLE IF NOT EXISTS database (
      id INTEGER NOT NULL PRIMARY KEY,
      name TEXT NOT NULL,
      value TEXT NOT NULL,
      date TEXT NOT NULL
    );
  ''');

Then use .toString() for DateTime:

final stmt =
        db.prepare('INSERT INTO database (name, value, date) VALUES (?, ?, ?)');
    stmt.execute(
        ['The Beatles', '31', DateTime.now().toString().split(".")[0]]);

Finally use datetime() function to convert String to Date in sqlite:

  final sql3.ResultSet resultSet = db.select(
        "SELECT * FROM database WHERE datetime(date) >= datetime('2025-04-03 00:00:00') AND datetime(date) <= datetime('2025-04-05 00:00:00')"); 

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.