Skip to content

Instantly share code, notes, and snippets.

@omarzer0
Created January 12, 2023 16:04
Show Gist options
  • Save omarzer0/e24e21df7feb97e136773464f810facf to your computer and use it in GitHub Desktop.
Save omarzer0/e24e21df7feb97e136773464f810facf to your computer and use it in GitHub Desktop.
Sqlfilte session
import 'package:flutter/material.dart';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
void main() {
runApp(MaterialApp(
debugShowCheckedModeBanner: false,
home: TestBB(),
));
}
class TestBB extends StatelessWidget {
void buildDB() async {
var path = await getDatabasesPath();
var dbPath = join(path, "test.db");
print(dbPath);
var db = await openDatabase(
dbPath,
version: 1,
onCreate: (db, version) {
db.execute(
'CREATE TABLE Users (id INTEGER PRIMARY KEY, name TEXT ,email TEXT, mobile TEXT)');
},
onUpgrade: (db, oldVersion, newVersion) {
// db.execute("ALTER TABLE Users ADD Address TEXT");
},
);
await db.insert("Users", {
'name': 'koko',
'email': '[email protected]',
});
await db.insert("Users", {
'name': 'fofo',
'email': '[email protected]',
});
await db.update("Users", {"name": "Omar"}, where: "id=1");
var data = await db.query("Users");
print(data);
print(data[0]["name"]);
await db.delete("Users", where: "name = 'koko'");
await db.delete("Users", where: "name = ?", whereArgs: ["fofo"]);
var data2 = await db.query("Users");
print("After deletion\n");
print(data2);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ElevatedButton(
onPressed: () {
buildDB();
},
child: Text("Click me"),
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment