Skip to content

Instantly share code, notes, and snippets.

@rajajawahar
Last active August 15, 2018 10:17
Show Gist options
  • Save rajajawahar/7f080ea850b620b140a61a84da041b0e to your computer and use it in GitHub Desktop.
Save rajajawahar/7f080ea850b620b140a61a84da041b0e to your computer and use it in GitHub Desktop.
import 'dart:async';
import 'dart:io' as io;
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path_provider/path_provider.dart';
class DBHelper{
static Database _db;
Future<Database> get db async {
if(_db != null)
return _db;
_db = await initDb();
return _db;
}
//Creating a database with name test.dn in your directory
initDb() async {
io.Directory documentsDirectory = await getApplicationDocumentsDirectory();
String path = join(documentsDirectory.path, "test.db");
var theDb = await openDatabase(path, version: 1, onCreate: _onCreate);
return theDb;
}
// Creating a table name Employee with fields
void _onCreate(Database db, int version) async {
// When creating the db, create the table
await db.execute(
"CREATE TABLE Employee(id INTEGER PRIMARY KEY, firstname TEXT, lastname TEXT, mobileno TEXT,emailId TEXT )");
print("Created tables");
}
// Retrieving employees from Employee Tables
Future<List<Employee>> getEmployees() async {
var dbClient = await db;
List<Map> list = await dbClient.rawQuery('SELECT * FROM Employee');
List<Employee> employees = new List();
for (int i = 0; i < list.length; i++) {
employees.add(new Employee(list[i]["firstname"], list[i]["lastname"], list[i]["mobileno"], list[i]["emailid"]));
}
print(employees.length);
return employees;
}
void saveEmployee(Employee employee) async {
var dbClient = await db;
await dbClient.transaction((txn) async {
return await txn.rawInsert(
'INSERT INTO Employee(firstname, lastname, mobileno, emailid ) VALUES(' +
'\'' +
employee.firstName +
'\'' +
',' +
'\'' +
employee.lastName +
'\'' +
',' +
'\'' +
employee.mobileNo +
'\'' +
',' +
'\'' +
employee.emailId +
'\'' +
')');
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment