I have an external database (localhost) that contains a "questions" table characterized by their level (Easy, Medium, Hard) and class(1st class, 2nd class, 3rd class).
I have an internal database that contains a "student" table with level and class attributes that contain in the external database.
I want to post only the questions that correspond to the level and class of the student that is already added in the internal database "SQFLite".
I posted all the questions in the JSON form but did not match the level and class of the student,
But I want when I add a new student with their level and class in the internal database, the questions that are displayed must be matched to that of the student.
model.dart: contains the student's model
class Student {
int _id;
String _name;
String _level;
String _classes ;
String _date;
Student(this._name, this._level, this._classes, this._date);
Student.withId(this._id, this._name, this._level, this._classes ,this._date);
String get date => _date;
String get level => _level;
String get classes => _classes;
String get name => _name;
int get id => _id;
set date(String value) {
_date = value;
}
set level(String value) {
_level = value;
}
set classes(String value) {
_classes = value;
}
set name(String value) {
if (value.length <= 255) {
_name = value;
}
}
Map<String, dynamic> toMap() {
var map = Map<String, dynamic>();
map["id"] = this._id;
map["name"] = this._name;
map["level"] = this._level;
map["classes"] = this._classes;
map["date"] = this._date;
return map;
}
Student.getMap(Map<String, dynamic> map){
this._id = map["id"];
this._name = map["name"];
this._level = map["level"];
this._classes= map["classes"];
this._date = map["date"];
}
}
sql_helper.dart: contains the internal database
import 'package:flutter/material.dart';
import 'package:sqflite/sqflite.dart';
import 'dart:async';
import 'dart:io';
import 'model.dart';
import 'package:path_provider/path_provider.dart';
class SQL_Helper {
static SQL_Helper dbHelper;
static Database _database;
SQL_Helper._createInstance();
factory SQL_Helper() {
if (dbHelper == null) {
dbHelper = SQL_Helper._createInstance();
}
return dbHelper;
}
String tableName = "students_table";
String _id = "id";
String __name = "name";
String __level= "level";
String __classes = "classes";
String __date = "date";
Future<Database> get database async {
if (_database == null){
_database = await initializedDatabase();
}
return _database;
}
Future<Database> initializedDatabase() async {
Directory directory = await getApplicationDocumentsDirectory();
String path = directory.path + "students.db";
var studentDB = await openDatabase(path, version: 1, onCreate: createDatabase);
return studentDB;
}
void createDatabase(Database db, int version) async {
await db.execute(
"CREATE TABLE $tableName($_id INTEGER PRIMARY KEY AUTOINCREMENT, $__name TEXT, $__level TEXT,$__classes TEXT, $__date TEXT )");
}
Future<List<Map<String, dynamic>>> getStudentMapList() async {
Database db = await this.database;
var result = await db.query(tableName, orderBy: "$_id ASC");
return result;
}
Future<int> insertStudent(Student student) async {
Database db = await this.database;
var result = await db.insert(tableName, student.toMap());
return result;
}
Future<int> updateStudent(Student student) async{
Database db = await this.database;
var result = await db.update(tableName, student.toMap(), where: "$_id = ?", whereArgs: [student.id]);
return result;
}
Future<int> deleteStudent(int id) async {
var db = await this.database;
int result = await db.rawDelete("DELETE FROM $tableName WHERE $_id = $id");
return result;
}
Future<int> getCount() async {
Database db = await this.database;
List<Map<String, dynamic>> all = await db.rawQuery("SELECT COUNT (*) FROM $tableName");
int result = Sqflite.firstIntValue(all);
return result;
}
Future<List<Student>> getStudentList() async{
var studentMapList = await getStudentMapList();
int count = studentMapList.length;
List<Student> students = new List<Student>();
for (int i = 0; i <= count -1; i++){
students.add(Student.getMap(studentMapList[i]));
}
return students;
}
}
I have an external database (localhost) that contains a "questions" table characterized by their level (Easy, Medium, Hard) and class(1st class, 2nd class, 3rd class). I have an internal database that contains a "student" table with level and class attributes that contain in the external database. I want to post only the questions that correspond to the level and class of the student that is already added in the internal database "SQFLite". I posted all the questions in the JSON form but did not match the level and class of the student, But I want when I add a new student with their level and class in the internal database, the questions that are displayed must be matched to that of the student.
model.dart: contains the student's model
sql_helper.dart: contains the internal database
main.dart