Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. import 'package:sqflite/sqflite.dart';
  2. import 'package:path/path.dart';
  3.  
  4. final String table = "contactTable";
  5. final String idColumn = "idColumn";
  6. final String nameColumn = "nameColumn";
  7. final String emailColumn = "emailColumn";
  8. final String phoneColumn = "phoneColumn";
  9. final String imgColumn = "imgColumn";
  10.  
  11. class ContactHelper {
  12. static final ContactHelper _instance = ContactHelper.internal();
  13. factory ContactHelper() => _instance;
  14. ContactHelper.internal();
  15.  
  16. Database _db;
  17. Future<Database> get db async {
  18. if (_db == null) {
  19. _db = await initDb();
  20. }
  21. return _db;
  22. }
  23.  
  24. Future<Database> initDb() async {
  25. final databasesPath = await getDatabasesPath();
  26. final path = join(databasesPath, "contacts.db");
  27. return await openDatabase(path, version: 1,
  28. onCreate: (Database db, int newerVersion) async {
  29. await db.execute('''CREATE TABLE $table (
  30. $idColumn INTEGER PRIMARY KEY,
  31. $nameColumn TEXT,
  32. $emailColumn TEXT,
  33. $phoneColumn TEXT,
  34. $imgColumn TEXT
  35. )''');
  36. });
  37. }
  38.  
  39. Future<Contact> saveContact(Contact contact) async {
  40. Database dbContact = await this.db;
  41. contact.id = await dbContact.insert(table, contact.toMap());
  42. return contact;
  43. }
  44.  
  45. Future<Contact> getContact(int id) async {
  46. Database dbContact = await this.db;
  47. List<Map> maps = await dbContact.query(table,
  48. columns: [
  49. idColumn,
  50. nameColumn,
  51. emailColumn,
  52. phoneColumn,
  53. imgColumn,
  54. ],
  55. where: "$idColumn = ?",
  56. whereArgs: [id]);
  57. if (maps.length > 0) {
  58. return Contact.fromMap(maps.first);
  59. } else {
  60. return null;
  61. }
  62. }
  63.  
  64. Future<List<Contact>> getAll() async {
  65. Database dbContact = await this.db;
  66. List<Map> maps = await dbContact.rawQuery("SELECT * FROM $table");
  67. List<Contact> list = List<Contact>();
  68. for (Map m in maps) {
  69. list.add(Contact.fromMap(m));
  70. }
  71. return list;
  72. }
  73.  
  74. Future<int> deleteContact(int id) async {
  75. Database dbContact = await this.db;
  76. return await dbContact.delete(table, where: "$idColumn = ?", whereArgs: [id]);
  77. }
  78.  
  79. Future<int> updateContact(Contact contact) async {
  80. Database dbContact = await this.db;
  81. return await dbContact.update(table, contact.toMap(), where: "$idColumn = ?", whereArgs: [contact.id]);
  82. }
  83.  
  84. Future<int> getNumber() async {
  85. Database dbContact = await this.db;
  86. return Sqflite.firstIntValue(await dbContact.rawQuery("SELECT COUNT(*) FROM $table"));
  87. }
  88.  
  89. Future closer() async {
  90. Database dbContact = await this.db;
  91. dbContact.close();
  92. }
  93. }
  94.  
  95. class Contact {
  96. int id;
  97. String name;
  98. String email;
  99. String phone;
  100. String img;
  101.  
  102. Contact();
  103.  
  104. Contact.fromMap(Map map) {
  105. this.id = map[idColumn];
  106. this.name = map[nameColumn];
  107. this.email = map[emailColumn];
  108. this.phone = map[phoneColumn];
  109. this.img = map[imgColumn];
  110. }
  111.  
  112. Map toMap() {
  113. Map<String, dynamic> maps = {
  114. nameColumn: this.name,
  115. emailColumn: this.email,
  116. phoneColumn: this.phone,
  117. imgColumn: this.img,
  118. };
  119. if (this.id != null) maps[idColumn] = this.id;
  120. return maps;
  121. }
  122.  
  123. @override
  124. String toString() {
  125. return "Contact: id = $id, name = $name, email = $email, phone = $phone, img = $img";
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement