crazy_db 1.0.4 crazy_db: ^1.0.4 copied to clipboard
use laravel api secondary packaging sqflite It's easier for newcomers to use
import 'package:flutter/material.dart';
import 'package:crazy_db/crazy_db.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
testDb();
}
@override
Widget build(BuildContext context) {
return Container();
}
void testDb() async {
await CrazyDB.connection("crazyDB.sqlite");
int id = await CrazyDB.table("request_cache")
.create({"url":"https://baidu.com"});
int count1 = await CrazyDB.table("request_cache")
.where(column: "url", value: "https://baidu.com")
.delete();
int count2 = await CrazyDB.table("request_cache")
.where(column: "url", value: "https://baidu.com")
.update({"json":"{}"});
//Many to many
List list = await CrazyDB.table("request_cache")
.join(
"request_detail", "request_cache.id", "=", "request_detail.url_id")
.join(
"request_time", "request_detail.id", "=", "request_time.detail_id")
.where(column: "id", value: 1)
.where(column: "url", value: "https://baidu.com")
.whereNotNull(column: "url")
.orWhere(column: "id", value: 2)
.whereNotBetween("id",[500,600])
.orderByDesc("id")
.whereNotIn("id", [1,3])
.where(
column: "request_time.create_time",
symbol: "<",
value: "2022-6-10 15:47")
.get(list: [
"request_cache.id",
"request_cache.url",
"request_cache.json",
"request_detail.id as detail_id",
"request_detail.title",
"request_detail.content",
"request_time.auth",
"request_time.create_time"
]);
List? list1 = await CrazyDB.table("request_cache")
.where(column: "id",value: "1")
.groupBy("id")
.having("count > 2")
.having("json = '{}'")
.get();
//print sql
CrazyDB.getSql();
//transaction
try {
CrazyDB.transaction();
CrazyDB.table("request_cache").create({"url":"https://sina.cn"});
Map map = await CrazyDB.table("request_cache").select().where(column: "id", value: 1).first();
if(map.isNotEmpty){
CrazyDB.table("request_cache").where(column: "id", value: 1).delete();
}
await CrazyDB.commit();
} catch (e) {
CrazyDB.rollback();
debugPrint("transaction=>$e");
}
await CrazyDB.table("request_cache").create({"url":"https://baidu.cn"});
debugPrint("list1=>$list1");
debugPrint(list.toString());
}
}