Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter delete all rows from sqllite

Tags:

flutter

dart

Need to delete all rows from SQLite

This is the file where I have all function of the database

class DatabaseHelper {

  static final _databaseName = "MyDatabase.db";
  static final _databaseVersion = 1;

  static final table = 'cart_table';

  // make this a singleton class
  DatabaseHelper._privateConstructor();
  static final DatabaseHelper instance = DatabaseHelper._privateConstructor();

  // only have a single app-wide reference to the database
  static Database _database;
  Future<Database> get database async {
    if (_database != null) return _database;
    // lazily instantiate the db the first time it is accessed
    _database = await _initDatabase();
    return _database;
  }

  // this opens the database (and creates it if it doesn't exist)
  _initDatabase() async {
    Directory documentsDirectory = await getApplicationDocumentsDirectory();
    String path = join(documentsDirectory.path, _databaseName);
    return await openDatabase(path,
        version: _databaseVersion,
        onCreate: _onCreate);
  }

  // SQL code to create the database table
  Future _onCreate(Database db, int version) async {
    await db.execute("CREATE TABLE $table ("
    "uId INTEGER PRIMARY KEY,"
        "id INTEGER,"
        "title TEXT,"
    "image TEXT,"
    "color TEXT,"
    "price TEXT,"
    "sizeselect TEXT"
    ")");


  }


  Future<int> insert(cart) async {
    print(cart.id);
    Database db = await instance.database;
    return await db.insert(table, cart.toJson());
  }

  Future<List<Map<String, dynamic>>> queryAllRows() async {
    Database db = await instance.database;
    return await db.query(table);
  }

  Future<int> queryRowCount() async {
    Database db = await instance.database;
    return Sqflite.firstIntValue(await db.rawQuery('SELECT COUNT(*) FROM $table'));
  }

  Future<int> delete(int id, columnId) async {
    print(columnId);
    print(id);
    Database db = await instance.database;
    return await db.delete(table, where: '$columnId = id');
  }

  deleteAll() async {
    Database db = await instance.database;
    return await db.rawDelete("Delete * from $table");
  }
}

I need to delete all rows. So I am doing like this

 final dbHelper = DatabaseHelper.instance;

 allRows = await dbHelper.queryAllRows();

it showing this error Unhandled Exception: DatabaseException(near "*": syntax error (code 1 SQLITE_ERROR): , while compiling: Delete * from cart_table) sql 'Delete * from cart_table' args []}

I am not getting point why it's showing an error because no need to pass anything and it's showing an error. What I need is just to delete the whole table

like image 786
rameez khan Avatar asked Jun 28 '26 10:06

rameez khan


2 Answers

Try to call delete operation without *:

delete from cart_table
like image 126
fartem Avatar answered Jul 02 '26 07:07

fartem


no need to use *. simply call like the code below

clearUserTable() async {
    Database db = await instance.database;
    return await db.rawDelete("DELETE FROM $tableName");
  }
like image 26
Tushar Ahmed Avatar answered Jul 02 '26 07:07

Tushar Ahmed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!