Advertisement
ZawXtut

sql Flutter DatabaseHelper Class

Jul 4th, 2022
1,490
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import 'package:address_book/database/address_database.dart';
  2. import 'package:address_book/database/db_conv.dart';
  3. import 'package:path/path.dart';
  4. import 'package:path_provider/path_provider.dart';
  5. import 'package:sqflite/sqflite.dart';
  6.  
  7. class DatabaseHelper {
  8.   String dbName = 'addressbook.db';
  9.   String tbName = 'books';
  10.   final COLUMN_ID = 'id';
  11.   final COLUMN_Name = 'name';
  12.   final COLUMN_Address = 'address';
  13.   final COLUMN_Phone = 'phone';
  14.   final COLUMN_Viber = 'viber';
  15.   final COLUMN_Facebook = 'facebook';
  16.   final COLUMN_Mail = 'mail';
  17.   final COLUMN_Image = 'image';
  18.   final COLUMN_Serial = 'serial';
  19.   final COLUMN_Roll = 'roll';
  20.  
  21.   static DatabaseHelper? databaseHelper;
  22.   DatabaseHelper._create();
  23.   Database? _database;
  24.  
  25. //Todo Check Database
  26.   factory DatabaseHelper() {
  27.     if (databaseHelper == null) {
  28.       databaseHelper = DatabaseHelper._create();
  29.     }
  30.     return databaseHelper!;
  31.   }
  32.   Future<Database> getDatabase() async {
  33.     if (_database == null) {
  34.       _database = await initDatabase();
  35.     }
  36.     return _database!;
  37.   }
  38.  
  39.   Future<Database> delSaveData(int? id) async {
  40.     return _database!;
  41.   }
  42.  
  43. //todo clear database
  44.   Future<Database> clearAllData() async {
  45.     return _database!;
  46.   }
  47.  
  48.   Future<Database> initDatabase() async {
  49.     // var directory = await getDatabasesPath();
  50.     var directory = await getApplicationDocumentsDirectory();
  51.     var db = join(directory.path, dbName);
  52.     return openDatabase(db, version: 1, onCreate: _create);
  53.   }
  54.  
  55. //Todo Id Del
  56.   deleteData(int id) async {
  57.     Database db = await delSaveData(id);
  58.     return db.delete(tbName, where: 'id=?', whereArgs: [id]);
  59.   }
  60.  
  61. //Todo create DataBase
  62.   Future<void> _create(Database database, version) async {
  63.     String sql =
  64.         'CREATE TABLE $tbName($COLUMN_ID INTEGER PRIMARY KEY AUTOINCREMENT,$COLUMN_Name VARCHAR,$COLUMN_Address VARCHAR,$COLUMN_Phone VARCHAR,$COLUMN_Viber VARCHAR,$COLUMN_Facebook VARCHAR,$COLUMN_Mail VARCHAR,$COLUMN_Image VARCHAR,$COLUMN_Serial VARCHAR ,$COLUMN_Roll VARCHAR)';
  65.     database.execute(sql);
  66.   }
  67.  
  68.   //Todo Save
  69.  
  70.   Future<int> save(AddressDatabase addressDatabase) async {
  71.     Database db = await getDatabase();
  72.     return db.insert(tbName, addressDatabase.toMap());
  73.   }
  74.  
  75. //Todo Get Data
  76.   Future<List<AddressDatabase>> getStudantData() async {
  77.     Database db = await getDatabase();
  78.     var bookMap = await db.query(tbName);
  79.     List<AddressDatabase> dataList =
  80.         bookMap.map((e) => AddressDatabase.fromMap(e)).toList();
  81.     return dataList;
  82.   }
  83.  
  84.   //todo Db count
  85.   Future<int> contactCount() async {
  86.     Database db = await getDatabase();
  87.     //var result = await db.query(tbName);
  88.     var x = await db.rawQuery('SELECT COUNT (*) FROM $tbName');
  89.     int? count = Sqflite.firstIntValue(x);
  90.     print("DB ==> $count");
  91.     return count!;
  92.   }
  93. }
  94.  
Advertisement
RAW Paste Data Copied
Advertisement