Search code examples
flutterdartsqflite

SqfliteDatabaseException (DatabaseException(near


How can I fix this?

SqfliteDatabaseException (DatabaseException(near "@gmail": syntax error (Sqlite code 1 SQLITE_ERROR): , while compiling: SELECT * FROM Users WHERE email = sonerbascifcii@gmail.com password = son123, (OS error - 2:No such file or directory)) sql 'SELECT * FROM Users WHERE email = sonerbascifcii@gmail.com password = son123' args [])

This is the code i came across.

  static Future<User> getLoginUser(String email, String password) async {
final db = await instance.database;
var result = await db.rawQuery("SELECT * FROM $users WHERE "
    "${UserTitle.email} = $email "
    "${UserTitle.password} = $password");

if (result.isNotEmpty) {
  return User.fromJson(result.first);
} else {
  return throw Exception('get LoginUser kısmında hata');
}

}


Solution

  • I think you have a quote issue and a missing logical operator issue in your SQL resquest, try like this:

    SELECT * FROM Users WHERE email = 'sonerbascifcii@gmail.com' AND password = 'son123'
    

    IMPORTANT

    Avoid use of unencrypted password, is is a security issue. You should encrypted your password and compare encrypted password, not clear ones.

    You should also use parameter query to avoid SQL injection:

    var result = await db.rawQuery("SELECT * FROM $users WHERE ${UserTitle.email} = ? AND ${UserTitle.password} = ? ", [$email, $password]);