Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
968 views
in Technique[技术] by (71.8m points)

dart - Flutter delete all rows from sqllite

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

question from:https://stackoverflow.com/questions/65939693/flutter-delete-all-rows-from-sqllite

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try to call delete operation without *:

delete from cart_table

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...