Home > Blockchain >  Sqflite, "no such column", when querying conditionally, even though it clearly exist when
Sqflite, "no such column", when querying conditionally, even though it clearly exist when

Time:10-25

The table:

void _createTablePomodoro(Batch batch) {
        batch.execute('DROP TABLE IF EXISTS pomodoro');
        batch.execute('''CREATE TABLE pomodoro (
        id INTEGER,
        date INTEGER,
        time INTEGER,
        tag STRING,
        PRIMARY KEY(id, date)
    )''');}

When I print out my table without my second condition (this one works perfect):

List results = await db.rawQuery(
    '''SELECT * FROM pomodoro WHERE (date BETWEEN ${start.millisecondsSinceEpoch} AND ${end.millisecondsSinceEpoch})''');
print(results);

Prints this:

-> [{id: null, date: 1666627397809, time: 27, tag: Home}, {id: null, date: 1666627397809, time: 27, tag: Home}]

Problem: When I add a condition and query by tag aswell:

List results = await db.rawQuery('''SELECT * FROM pomodoro WHERE (tag = Home) AND (date BETWEEN ${start.millisecondsSinceEpoch} AND ${end.millisecondsSinceEpoch})''');

I get this exception: SqfliteDatabaseException (DatabaseException(no such column: Home (code 1 SQLITE_ERROR[1]): , while compiling: SELECT * FROM pomodoro WHERE tag = Home OR (date BETWEEN 1666562400000 AND 1666648800000)) sql 'SELECT * FROM pomodoro WHERE tag = Home OR (date BETWEEN 1666562400000 AND 1666648800000)' args [])

What is happening here? I can literally see that the column exists from the first print statement, when I replace tag with id or time, it works aswell, so why isn't it working specifically for tag?

CodePudding user response:

You should add quotes to the Home value. Like this:

List results = await db.rawQuery('''SELECT * FROM pomodoro WHERE (tag = "Home") AND (date BETWEEN ${start.millisecondsSinceEpoch} AND ${end.millisecondsSinceEpoch})''');
  • Related