sqflite插件简单使用

sqflite是github上tekartik提供的可供Android和iOS使用的SQLite插件,操作也是非常的简单.

下载

dependencies:
  
  ...

  sqflite: ^1.1.0

导入

import 'package:sqflite/sqflite.dart';

用SQL进行操作

获取资源存放位置

import 'package:path/path.dart';
// join 使用这个包

String _path = '';

Future getPath() async {
  return await getDatabasesPath();
}

// 使用
getPath().then((path) {
  setState(() {
    _path = join(path.toString(), 'demo.db');
  });
});

建表

Database _database;
Future createDB() async {
    _database = await openDatabase(
      _path,
      version: 1,
      onCreate: (db, version) async {
        await db.execute(
            'CREATE TABLE demo(id INTEGER PRIMARY KEY, name TEXT, value INTEGER, num REAL)');
      },
    );
  }

插入一条数据

_database.transaction((txn) async {
  await txn.rawInsert(
     'INSERT INTO demo(name, value, num) VALUES("some name", 1234, 456.789)');
})

查询数据

Future<List> query() async {
    return await _database.rawQuery('SELECT * FROM demo');
  }

修改数据

Future update() async {
    return await _database.rawUpdate(
        'UPDATE demo SET name = ?, VALUE = ? WHERE name = ?',
        ['updated name', '9876', 'some name']);
  }

删除一条数据

Future delete() async {
    return await _database
        .rawDelete('DELETE FROM demo WHERE 1 ORDER BY id LIMIT 1');
  }

使用对象进行操作

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:sqflite/sqflite.dart';

/*
 * @Date: 2019-02-28 10:24 
 * @Description TODO
 */

final String tableTodo = 'todo';
final String columnId = '_id';
final String columnTitle = 'title';
final String columnDone = 'done';

class Todo {
  int id;
  String title;
  bool done;

  Map<String, dynamic> toMap() {
    var map = <String, dynamic>{
      columnTitle: title,
      columnDone: done ? 0 : 1,
    };

    if (id != null) {
      map[columnId] = id;
    }
    return map;
  }

  Todo();

  Todo.fromMap(Map<String, dynamic> map) {
    id = map[columnId];
    title = map[columnTitle];
    done = map[columnDone] == 1;
  }
}

class TodoProvider {
  Database db;

  Future open(String path) async {
    db = await openDatabase(
      path,
      version: 1,
      onCreate: (db, version) async {
        await db.execute('''
create table $tableTodo ( 
  $columnId integer primary key autoincrement, 
  $columnTitle text not null,
  $columnDone integer not null)
''');
      },
    );
  }

  Future<Todo> insert(Todo todo) async {
    todo.id = await db.insert(tableTodo, todo.toMap());
    return todo;
  }

  Future<Todo> getTodo(int id) async {
    List<Map> map = await db.query(
      tableTodo,
      columns: [columnId, columnTitle, columnTitle],
      where: '$columnId = ?',
      whereArgs: [id],
    );

    if(map.length > 0) {
      return Todo.fromMap(map.first);
    }
    return null;
  }

  Future<int> delete(int id) async {
    return await db.delete(tableTodo, where: '$columnId = ?', whereArgs: [id]);
  }
  
  Future<int> update(Todo todo) async {
    return await db.update(tableTodo, todo.toMap(), where: '$columnId = ?', whereArgs: [todo.id]);
  }

  Future close() async => db.close();
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值