parent
306a273a8c
commit
a7ec1b63c2
@ -0,0 +1,103 @@
|
|||||||
|
import 'dart:io';
|
||||||
|
|
||||||
|
import 'package:flustars/flustars.dart';
|
||||||
|
import 'package:path_provider/path_provider.dart';
|
||||||
|
import 'package:project_telephony/model/send_number_model.dart';
|
||||||
|
import 'package:sqflite/sqflite.dart';
|
||||||
|
import 'package:path/path.dart';
|
||||||
|
|
||||||
|
class DBManager {
|
||||||
|
/// 数据库名
|
||||||
|
final String _dbName = "dbName";
|
||||||
|
|
||||||
|
/// 数据库版本
|
||||||
|
final int _version = 1;
|
||||||
|
|
||||||
|
static final DBManager _instance = DBManager._();
|
||||||
|
|
||||||
|
factory DBManager() {
|
||||||
|
return _instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
DBManager._();
|
||||||
|
|
||||||
|
static Database? _db;
|
||||||
|
|
||||||
|
Future<Database> get db async {
|
||||||
|
return _db ??= await _initDB();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 初始化数据库
|
||||||
|
Future<Database> _initDB() async {
|
||||||
|
Directory directory = await getApplicationDocumentsDirectory();
|
||||||
|
String path = join(directory.path, _dbName);
|
||||||
|
return await openDatabase(
|
||||||
|
path,
|
||||||
|
version: _version,
|
||||||
|
onCreate: _onCreate,
|
||||||
|
onUpgrade: _onUpgrade,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 创建表
|
||||||
|
Future _onCreate(Database db, int version) async {
|
||||||
|
const String sql = """
|
||||||
|
CREATE TABLE SendNumber(
|
||||||
|
id INTEGER primary key AUTOINCREMENT,
|
||||||
|
sendNumber TEXT,
|
||||||
|
createdAt datetime
|
||||||
|
)
|
||||||
|
""";
|
||||||
|
return await db.execute(sql);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 更新表
|
||||||
|
Future _onUpgrade(Database db, int oldVersion, int newVersion) async {}
|
||||||
|
|
||||||
|
/// 保存数据
|
||||||
|
Future saveData(SendNumber sendNumber) async {
|
||||||
|
Database database = await db;
|
||||||
|
return await database.insert("SendNumber", sendNumber.toJson());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 使用SQL保存数据
|
||||||
|
Future saveDataBySQL(SendNumber sendNumber) async {
|
||||||
|
const String sql = """
|
||||||
|
INSERT INTO SendNumber(sendNumber,createdAt) values(?,?)
|
||||||
|
""";
|
||||||
|
Database database = await db;
|
||||||
|
return await database.rawInsert(sql, [sendNumber.sendNumber, sendNumber.createdAt]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 查询全部数据
|
||||||
|
Future<List<SendNumber>?> findAll() async {
|
||||||
|
Database? database = await db;
|
||||||
|
List<Map<String, Object?>> result = await database.query("SendNumber");
|
||||||
|
if (result.isNotEmpty) {
|
||||||
|
return result.map((e) => SendNumber.fromJson(e)).toList();
|
||||||
|
} else {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///条件查询
|
||||||
|
Future<List<SendNumber>?> find(String sendNumber) async {
|
||||||
|
Database database = await db;
|
||||||
|
List<Map<String, Object?>> result =
|
||||||
|
await database.query("SendNumber", where: "sendNumber=?", whereArgs: [sendNumber]);
|
||||||
|
if (result.isNotEmpty) {
|
||||||
|
return result.map((e) => SendNumber.fromJson(e)).toList();
|
||||||
|
} else {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 修改
|
||||||
|
Future<int> update(SendNumber sendNumber) async {
|
||||||
|
Database database = await db;
|
||||||
|
sendNumber.createdAt = DateUtil.formatDate(DateTime.now());
|
||||||
|
int count =
|
||||||
|
await database.update("SendNumber", sendNumber.toJson(), where: "sendNumber=?", whereArgs: [sendNumber.sendNumber]);
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
import 'dart:convert';
|
||||||
|
|
||||||
|
SendNumber sendNumberFromJson(String str) => SendNumber.fromJson(json.decode(str));
|
||||||
|
|
||||||
|
String sendNumberToJson(SendNumber data) => json.encode(data.toJson());
|
||||||
|
|
||||||
|
class SendNumber {
|
||||||
|
SendNumber({
|
||||||
|
this.sendNumber,
|
||||||
|
this.createdAt,
|
||||||
|
});
|
||||||
|
|
||||||
|
SendNumber.fromJson(dynamic json) {
|
||||||
|
sendNumber = json['sendNumber'];
|
||||||
|
createdAt = json['createdAt'];
|
||||||
|
}
|
||||||
|
|
||||||
|
String? sendNumber;
|
||||||
|
String? createdAt;
|
||||||
|
|
||||||
|
Map<String, dynamic> toJson() {
|
||||||
|
final map = <String, dynamic>{};
|
||||||
|
map['sendNumber'] = sendNumber;
|
||||||
|
map['createdAt'] = createdAt;
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue