YSSqlite.dart 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import 'package:sqflite/sqflite.dart';
  2. String sqName = 'walletDb';
  3. class YSVideoTable{
  4. Database? db;
  5. String? tabName = 'videoList';
  6. YSVideoTable init() {
  7. final file = YSVideoTable();
  8. open();
  9. return file;
  10. }
  11. //判断表是否存在
  12. isTableExits() async {
  13. db ??= await openDatabase('$sqName.db');
  14. var sql ="SELECT * FROM sqlite_master WHERE TYPE = 'table' AND NAME = '$tabName'";
  15. var res = await db!.rawQuery(sql);
  16. var returnRes = res.isNotEmpty;
  17. return returnRes;
  18. }
  19. open() async {
  20. if (await isTableExits()==false) {
  21. execute();
  22. }
  23. }
  24. ///创建表
  25. execute() async{
  26. db ??= await openDatabase('$sqName.db');
  27. await db!.execute(
  28. 'CREATE TABLE $tabName (id INTEGER PRIMARY KEY, name TEXT, path TEXT, time TEXT)'
  29. );
  30. }
  31. ///插入数据
  32. Future<int> rawInsert({required String name,required String path,required String time}) async{
  33. db ??= await openDatabase('$sqName.db');
  34. int id = await db!.rawInsert(
  35. 'INSERT INTO $tabName(name,path,time) VALUES("$name","$path","$time")'
  36. );
  37. return id;
  38. }
  39. ///查询数据
  40. Future<List> rawQuery() async{
  41. try{
  42. db ??= await openDatabase('$sqName.db');
  43. List<Map> list = await db!.rawQuery('SELECT * FROM $tabName');
  44. return list;
  45. }catch(e){
  46. return [];
  47. }
  48. }
  49. ///更新数据
  50. Future<int> rawUpdate({required String id,required String name,required String path,required String time}) async{
  51. db ??= await openDatabase('$sqName.db');
  52. int count = await db!.rawUpdate(
  53. 'UPDATE $tabName SET name = ?,path = ?,time = ? ,to_token_addr = ?,amount = ? ,hash = ? WHERE id = $id',
  54. [name,path,time]
  55. );
  56. return count;
  57. }
  58. ///删除数据
  59. Future<int> rawDelete(String id) async{
  60. db ??= await openDatabase('$sqName.db');
  61. int count = await db!.rawDelete('DELETE FROM $tabName WHERE id = ?', [id]);
  62. return count;
  63. }
  64. Future<int> delete() async{
  65. db ??= await openDatabase('$sqName.db');
  66. final status = await db?.delete(tabName!)??0;
  67. return status;
  68. }
  69. }