import 'package:path_provider/path_provider.dart';
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';
import 'package:rxdart/rxdart.dart'; main() =>runApp(MaterialApp(home: MyApp(),)); class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return MyAppState();
}
} class MyAppState extends State<MyApp> {
TextEditingController _textController = TextEditingController(); Future createDB() async{
var path =(await getApplicationDocumentsDirectory()).path;
var dbPath = join(path, 'whois.db');
Database _database = await openDatabase(dbPath, version: 1, onCreate: (db, ver){
db.execute('CREATE TABLE whois (id INTEGER PRIMARY KEY, name TEXT)');
});
return _database;
}
//mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
Future addData (String table, Map<String, dynamic> sqlMap) async {
Database db = await createDB();
var a = await db.insert(table, sqlMap);
print('$a, done');
db.close();
} Future getData (String sqlString) async {
Database db = await createDB();
var b = await db.rawQuery(sqlString);
print(b);
db.close();
}
//mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
checkString () async {
await getData('SELECT * FROM whois');
} addString () async {
await addData('whois', {'name':'just testing'});
}
//mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('as'),),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text('ss'),
TextField(controller: _textController,),
RaisedButton(child: Text('add'), onPressed: addString,),
RaisedButton(child: Text('get'), onPressed: checkString,),
],
),
);
}
}

  

flutter + bloc + sqflite demo

import 'package:path_provider/path_provider.dart';
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';
import 'package:rxdart/rxdart.dart'; main() =>runApp(MaterialApp(home: MyApp(bloc: DataBloc(), db: DB(),),)); class MyApp extends StatelessWidget {
MyApp({this.bloc, this.db});
final DataBloc bloc;
final DB db;
TextEditingController _textController = TextEditingController(); add()async{
await db.addData('grant', {'name':'${_textController.text}'});
} query() async{
List result = await db.queryData('SELECT * FROM grant');
_textController.text = result[result.length-1]['name'];
} delete()async{
await db.deleteData('DELETE FROM grant WHERE id<4');
} @override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('test'),),
body: StreamBuilder(
stream: bloc.dataBloc.stream,
builder: (context, snapshot){
if (snapshot.hasData){
return Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text('head'),
TextField(controller: _textController,),
RaisedButton(child: Text('add'),onPressed: add,),
RaisedButton(child: Text('query'),onPressed: query,),
RaisedButton(child: Text('delete'),onPressed: delete,),
],
);
}else{
return Center(child:
RaisedButton(
child:Text('start'),
onPressed: (){
bloc.dataBloc.add('start');
},
),
);
}
}
),
);
}
} class DataBloc {
PublishSubject dataBloc = PublishSubject();
DataBloc(){
dataBloc.listen(onData);
}
void onData(value) {} } class DB { Future getDB() async{
var path = (await getApplicationDocumentsDirectory()).path;
var dbPath = join(path, 'abc', 'whois.db');
Database _database = await openDatabase(dbPath, version: 1, onCreate: (db, ver){
db.execute('CREATE TABLE grant (id INTEGER PRIMARY KEY, name TEXT)');
});
return _database;
} Future addData (String table, Map<String, dynamic> sqlMap) async {
Database db = await getDB();
var ret = await db.insert(table, sqlMap);
print('$ret done');
db.close();
return ret;
} Future queryData (String sql) async{
Database db = await getDB();
var ret = await db.rawQuery(sql);
db.close();
print('$ret done');
return ret;
} Future deleteData (String sql) async {
Database db = await getDB();
var ret = await db.rawDelete(sql);
db.close();
print('$ret done');
return ret;
} Future updateData (String sql) async{
Database db = await getDB();
var ret = await db.rawUpdate(sql);
db.close();
print('$ret done');
return ret;
} }

  

self sqflite sample =======================的更多相关文章

  1. Linux下UPnP sample分析

        一.UPnP简介   UPnP(Universal Plug and Play)技术是一种屏蔽各种数字设备的硬件和操作系统的通信协议.它是一种数字网络中间件技术,建立在TCP/IP.HTTP协 ...

  2. cocos2d-x for android配置 & 运行 Sample on Linux OS

    1.从http://www.cocos2d-x.org/download下载稳定版 比如cocos2d-x-2.2 2.解压cocos2d-x-2.2.zip,比如本文将其解压到 /opt 目录下 3 ...

  3. android studio2.2 的Find Sample Code点击没有反应

    1 . 出现的问题描述:           右键点击Find Sample Code后半天没有反应,然后提示 Samples are currently unavailable for :{**** ...

  4. jmeter(四)Sample之http请求

    启动jmeter,建立一个测试计划 这里再次说说怎么安装和启动jmeter吧,昨天下午又被人问到怎样安装和使用,我也是醉了:在我看来,百度能解决百分之八十的问题,特别是基础的问题... 安装:去官网下 ...

  5. jcaptcha sample 制作验证码

    Skip to end of metadata Created by marc antoine garrigue, last modified by Jeremy Waters on Feb 23, ...

  6. Python 对不均衡数据进行Over sample(重抽样)

    需要重采样的数据文件(Libsvm format),如heart_scale +1 1:0.708333 2:1 3:1 4:-0.320755 5:-0.105023 6:-1 7:1 8:-0.4 ...

  7. Basic linux command-with detailed sample

    Here I will list some parameters which people use very ofen, I will attach the output of the command ...

  8. 例子:RSS Reader Sample

    本例演示了Rss xml信息的获取,以及如何使用SyndicationFeed来进行符合Rss规范的xml进行解析. SyndicationFeed 解析完成后 可以得到SyndicationItem ...

  9. 例子:Background Audio Streamer Sample

    The Background Audio Streamer sample demonstrates how to create an app that uses a MediaStreamSource ...

随机推荐

  1. Ehcache 3.7文档—基础篇—GettingStarted

    为了使用Ehcache,你需要配置CacheManager和Cache,有两种方式可以配置java编程配置或者XML文件配置 一. 通过java编程配置 CacheManager cacheManag ...

  2. js 第三期 小肩膀 第一段

    var 时间戳 = new.Date().getTime();//系统引擎 var 时间戳 = Date.now(); //V8引擎 数据类型 false =  underfuned =null = ...

  3. VUE 安装&创建一个项目

    1,安装node.js vue依赖nodejs,所以首先要安装node.js 然后打开cmd,输入命令, node -v.正常出现版本号,说明你已经安装成功了 下载地址:http://nodejs.c ...

  4. char和QChar(Unicode的编码与内存里的值还不是一回事)

    char类型是c/c++中内置的类型,描述了1个字节的内存信息的解析.比如: char gemfield=’g’;那么在由gemfield标记的这块内存的大小就是1个字节,信息就是01100111,8 ...

  5. Mac下StarUML的安装以及破解

    1.下载地址:http://staruml.io/ 2. 打开 /Applications/StarUML.app/Contents/www/license/node/LicenseManagerDo ...

  6. Centos 安装Java jdk

    1/ yum search java|grep jdk 2/ yum install java-......... 3/ vi /etc/profile 在最后添上: 4/ source /etc/p ...

  7. ORA-00600: 内部错误代码, 参数: [kcm_headroom_warn_1], [], [], [], [], [], [], [], [], [], [], []

    SQL*Plus: Release 11.2.0.4.0 Production on 星期三 1月 1 08:53:48 2003 Copyright (c) 1982, 2013, Oracle. ...

  8. Java数组之二维数组

    Java中除了一维数组外,还有二维数组,三维数组等多维数组.本文以介绍二维数组来了解多维数组. 1.二维数组的基础 二维数组的定义:二维数组就是数组的数组,数组里的元素也是数组. 二维数组表示行列二维 ...

  9. 交叉编译7zip过程

    https://github.com/Distrotech/p7zip.git 从这里下载,不要从sourceforge.net上下载,那上面的缺makefile文件. 我主要把文件 makefile ...

  10. 使用MyEclipse新建maven项目时报An internal error occurred during: "Retrieving archetypes:". GC overhead limit

    前几天在上手maven时,遇到了一个十分头疼的问题,我的myeclipse配置的是自己安装的插件 ,总是报 " An internal error occurred during: &quo ...