1.首先需要导入在pubspec.yaml中导入sqflite
1)dependencies:
sqflite: ^1.0.0
https://pub.dartlang.org/packages/sqflite
2)执行 flutter packages get
3)导入 import 'package:sqflite/sqflite.dart';
2.为了保证对数据库操作的过程中不会创建多个实例,所以工具类需要使用单例模式
主要用到的是DatabaseHelper,数据库的增删查改和sqlite没区别,直接贴出代码块
注:其中使用到的Note.dart
class Note {
int _id;
int _pointid;
String _image;
int _contractId;
int _publicationId;
double _pointLng;
double _pointLat;
Note(this._pointid, this._image ,this._contractId,this._publicationId,this._pointLng,this._pointLat);
Note.map(dynamic obj) {
this._id = obj['id'];
this._pointid = obj['pointid'];
this._image = obj['image'];
this._contractId = obj['contractId'];
this._publicationId = obj['publicationId'];
this._pointLng = obj['pointLng'];
this._pointLat = obj['pointLat'];
}
int get id => _id;
int get pointid => _pointid;
String get image => _image;
int get contractId => _contractId;
int get publicationId => _publicationId;
double get pointLng => _pointLng;
double get pointLat => _pointLat;
Map<String, dynamic> toMap() {
var map = new Map<String, dynamic>();
if (_id != null) {
map['id'] = _id;
}
map['pointid'] = _pointid;
map['image'] = _image;
map['contractId'] = _contractId;
map['publicationId'] = _publicationId;
map['pointLng'] = _pointLng;
map['pointLat'] = _pointLat;
return map;
}
Note.fromMap(Map<String, dynamic> map) {
this._id = map['id'];
this._pointid = map['pointid'];
this._image = map['image'];
this._contractId = map['contractId'];
this._pointLng = map['pointLng'];
this._pointLat = map['pointLat'];
}
}
1)创建数据库
void _onCreate(Database db, int newVersion) async {
await db.execute(
'CREATE TABLE $tableNote($columnId INTEGER PRIMARY KEY AUTOINCREMENT, $pointid TEXT, $image TEXT, $contractId TEXT, $publicationId TEXT, $pointLng TEXT, $pointLat TEXT)');
}
2)增加一条数据
Future<int> saveNote(Note note) async {
var dbClient = await db;
var result = await dbClient.insert(tableNote, note.toMap());
// var result = await dbClient.rawInsert(
// 'INSERT INTO $tableNote ($columnTitle, $columnDescription) VALUES (\'${note.title}\', \'${note.description}\')');
return result;
}
3)获取数据库所有的数据
Future<List> getAllNotes() async {
var dbClient = await db;
var result = await dbClient.query(tableNote, columns: [columnId, pointid, image, contractId ,publicationId, pointLng, pointLat]);
return result.toList();
}
4)获取数据库数据的数量
Future<int> getCount() async {
var dbClient = await db;
return Sqflite.firstIntValue(await dbClient.rawQuery('SELECT COUNT(*) FROM $tableNote'));
}
5)获取数据库其中一条数据(键是id)
Future<Note> getNote(int id) async {
var dbClient = await db;
List<Map> result = await dbClient.query(tableNote,
columns: [columnId, pointid, image ,contractId, publicationId, pointLng, pointLat],
where: '$columnId = ?',
whereArgs: [id]);
// var result = await dbClient.rawQuery('SELECT * FROM $tableNote WHERE $columnId = $id');
if (result.length > 0) {
return new Note.fromMap(result.first);
}
return null;
}
6)删除数据库一条数据
Future<int> deleteNote(String images) async {
var dbClient = await db;
return await dbClient.delete(tableNote, where: '$image = ?', whereArgs: [images]);
// return await dbClient.rawDelete('DELETE FROM $tableNote WHERE $columnId = $id');
}
7)更改数据库一条数据
Future<int> updateNote(Note note) async {
var dbClient = await db;
return await dbClient.update(tableNote, note.toMap(), where: "$columnId = ?", whereArgs: [note.id]);
// return await dbClient.rawUpdate(
// 'UPDATE $tableNote SET $columnTitle = \'${note.title}\', $columnDescription = \'${note.description}\' WHERE $columnId = ${note.id}');
}
8)关闭数据库
Future close() async {
var dbClient = await db;
return dbClient.close();
}
最后贴出代码块的集合
import 'dart:async';
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';
import 'package:seller_app/model/Note.dart';
class DatabaseHelper {
static final DatabaseHelper _instance = new DatabaseHelper.internal();
factory DatabaseHelper() => _instance;
final String tableNote = 'note11Table';
final String columnId = 'id';
final String pointid = 'pointid';
final String image = 'image';
final String contractId = 'contractId';
final String publicationId = 'publicationId';
final String pointLat = 'pointLat';
final String pointLng = 'pointLng';
static Database _db;
DatabaseHelper.internal();
Future<Database> get db async {
if (_db != null) {
return _db;
}
_db = await initDb();
return _db;
}
initDb() async {
String databasesPath = await getDatabasesPath();
String path = join(databasesPath, 'notes11.db');
// await deleteDatabase(path); // just for testing
var db = await openDatabase(path, version: 1, onCreate: _onCreate);
return db;
}
void _onCreate(Database db, int newVersion) async {
await db.execute(
'CREATE TABLE $tableNote($columnId INTEGER PRIMARY KEY AUTOINCREMENT, $pointid TEXT, $image TEXT, $contractId TEXT, $publicationId TEXT, $pointLng TEXT, $pointLat TEXT)');
}
Future<int> saveNote(Note note) async {
var dbClient = await db;
var result = await dbClient.insert(tableNote, note.toMap());
// var result = await dbClient.rawInsert(
// 'INSERT INTO $tableNote ($columnTitle, $columnDescription) VALUES (\'${note.title}\', \'${note.description}\')');
return result;
}
Future<List> getAllNotes() async {
var dbClient = await db;
var result = await dbClient.query(tableNote, columns: [columnId, pointid, image, contractId ,publicationId, pointLng, pointLat]);
// var result = await dbClient.rawQuery('SELECT * FROM $tableNote');
return result.toList();
}
Future<int> getCount() async {
var dbClient = await db;
return Sqflite.firstIntValue(await dbClient.rawQuery('SELECT COUNT(*) FROM $tableNote'));
}
Future<Note> getNote(int id) async {
var dbClient = await db;
List<Map> result = await dbClient.query(tableNote,
columns: [columnId, pointid, image ,contractId, publicationId, pointLng, pointLat],
where: '$columnId = ?',
whereArgs: [id]);
// var result = await dbClient.rawQuery('SELECT * FROM $tableNote WHERE $columnId = $id');
if (result.length > 0) {
return new Note.fromMap(result.first);
}
return null;
}
Future<int> deleteNote(String images) async {
var dbClient = await db;
return await dbClient.delete(tableNote, where: '$image = ?', whereArgs: [images]);
// return await dbClient.rawDelete('DELETE FROM $tableNote WHERE $columnId = $id');
}
Future<int> updateNote(Note note) async {
var dbClient = await db;
return await dbClient.update(tableNote, note.toMap(), where: "$columnId = ?", whereArgs: [note.id]);
// return await dbClient.rawUpdate(
// 'UPDATE $tableNote SET $columnTitle = \'${note.title}\', $columnDescription = \'${note.description}\' WHERE $columnId = ${note.id}');
}
Future close() async {
var dbClient = await db;
return dbClient.close();
}
}
3.使用
1)导入工具类 例:
import 'package:seller_app/systemAssembly/DatabaseHelper.dart';
2)创建
var db = new DatabaseHelper();
3)
创建数据:
await db.saveNote(new Note(pointId, _image,contractId,publicationId,locationLng,locationLat));
获取所有的数据:
List notes;
print('=== getAllNotes() ===');
notes = await db.getAllNotes();
notes.forEach((note) => print(note));
删除某一条数据:
db.deleteNote(image); //image为数据库中某一条数据的image属性
好了,到这里就结束了。欢迎大家一起探讨flutter,有什么不明白的可以留言。本人也是flutter小白一枚,正在踩坑,文章逻辑也比较乱,将就看看吧,谢谢大家^_^