searchData.dart 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import 'package:sqflite/sqflite.dart';
  2. import 'dart:async';
  3. import 'dart:io';
  4. import 'package:path_provider/path_provider.dart';
  5. class SearchData {
  6. static SearchData _databaseHelper;
  7. static Database _database;
  8. final String tableSearch = 'TableSearch';
  9. final String columnId = 'id';
  10. final String title = 'title';
  11. SearchData._creatInstance();
  12. factory SearchData() {
  13. if (_databaseHelper == null) {
  14. _databaseHelper = SearchData._creatInstance();
  15. }
  16. return _databaseHelper;
  17. }
  18. Future<Database> initializeDatabase() async {
  19. Directory directory = await getApplicationDocumentsDirectory();
  20. String path = directory.path + 'search.db';
  21. var employeeDatabase =
  22. await openDatabase(path, version: 1, onCreate: _creatDB);
  23. return employeeDatabase;
  24. }
  25. void _creatDB(Database db, int newVersion) async {
  26. await db.execute(
  27. 'CREATE TABLE $tableSearch($columnId INTEGER PRIMARY KEY AUTOINCREMENT, $title TEXT)',
  28. );
  29. }
  30. Future<Database> get database async {
  31. if (_database == null) {
  32. _database = await initializeDatabase();
  33. }
  34. return _database;
  35. }
  36. Future<List<SearchModel>> getSearchList() async {
  37. Database db = await this.database;
  38. var result =
  39. await db.rawQuery("SELECT * FROM $tableSearch order by $columnId ASC");
  40. if (result == null || result.length == 0) {
  41. return null;
  42. }
  43. List<SearchModel> titles = List<SearchModel>();
  44. result.forEach((item) => titles.add(SearchModel.fromJson(item)));
  45. return titles.take(20).toList();
  46. }
  47. // Insert
  48. Future<int> insertSearch(SearchModel searchModel) async {
  49. Database db = await this.database;
  50. final searchList = await getSearchList();
  51. if (searchList != null &&
  52. searchList
  53. .where((element) => element.title == searchModel.title)
  54. .length >
  55. 0) {
  56. return 0;
  57. } else {
  58. var result = await db.insert(tableSearch, searchModel.toJson());
  59. return result;
  60. }
  61. }
  62. // Delete
  63. Future<int> deleteSearch(String titleSearch) async {
  64. var db = await this.database;
  65. int result = await db
  66. .rawDelete("DELETE FROM $tableSearch WHERE $title = '$titleSearch'");
  67. return result;
  68. }
  69. Future<int> deleteSearchList() async {
  70. var db = await this.database;
  71. int result = await db.rawDelete("DELETE FROM $tableSearch");
  72. return result;
  73. }
  74. }
  75. class SearchModel {
  76. int id;
  77. String title;
  78. SearchModel({this.id, this.title});
  79. SearchModel.fromJson(Map<String, dynamic> json) {
  80. id = json['id'];
  81. title = json['title'];
  82. }
  83. Map<String, dynamic> toJson() {
  84. final Map<String, dynamic> data = new Map<String, dynamic>();
  85. data['id'] = this.id;
  86. data['title'] = this.title;
  87. return data;
  88. }
  89. SearchModel.fromSql(Map<String, dynamic> json) {
  90. id = json['id'];
  91. title = json['title'];
  92. }
  93. }