123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- import 'package:sqflite/sqflite.dart';
- String sqName = 'walletDb';
- class YSVideoTable{
- Database? db;
- String? tabName = 'videoList';
- YSVideoTable init() {
- final file = YSVideoTable();
- open();
- return file;
- }
- //判断表是否存在
- isTableExits() async {
- db ??= await openDatabase('$sqName.db');
- var sql ="SELECT * FROM sqlite_master WHERE TYPE = 'table' AND NAME = '$tabName'";
- var res = await db!.rawQuery(sql);
- var returnRes = res.isNotEmpty;
- return returnRes;
- }
- open() async {
- if (await isTableExits()==false) {
- execute();
- }
- }
- ///创建表
- execute() async{
- db ??= await openDatabase('$sqName.db');
- await db!.execute(
- 'CREATE TABLE $tabName (id INTEGER PRIMARY KEY, name TEXT, path TEXT, time TEXT)'
- );
- }
- ///插入数据
- Future<int> rawInsert({required String name,required String path,required String time}) async{
- db ??= await openDatabase('$sqName.db');
- int id = await db!.rawInsert(
- 'INSERT INTO $tabName(name,path,time) VALUES("$name","$path","$time")'
- );
- return id;
- }
- ///查询数据
- Future<List> rawQuery() async{
- try{
- db ??= await openDatabase('$sqName.db');
- List<Map> list = await db!.rawQuery('SELECT * FROM $tabName');
- return list;
- }catch(e){
- return [];
- }
- }
- ///更新数据
- Future<int> rawUpdate({required String id,required String name,required String path,required String time}) async{
- db ??= await openDatabase('$sqName.db');
- int count = await db!.rawUpdate(
- 'UPDATE $tabName SET name = ?,path = ?,time = ? ,to_token_addr = ?,amount = ? ,hash = ? WHERE id = $id',
- [name,path,time]
- );
- return count;
- }
- ///删除数据
- Future<int> rawDelete(String id) async{
- db ??= await openDatabase('$sqName.db');
- int count = await db!.rawDelete('DELETE FROM $tabName WHERE id = ?', [id]);
- return count;
- }
- Future<int> delete() async{
- db ??= await openDatabase('$sqName.db');
- final status = await db?.delete(tabName!)??0;
- return status;
- }
- }
|