Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:io';
- import 'package:path_provider/path_provider.dart';
- import 'package:sqflite/sqflite.dart';
- import 'package:sqflite/sql.dart';
- import 'package:sqflite/sqlite_api.dart';
- import 'package:sqliteflutter/model/usuario.dart';
- class UserDatabaseProvider{
- UserDatabaseProvider._();
- static final UserDatabaseProvider db = UserDatabaseProvider._();
- Database _database;
- Future<Database> get database async {
- if(_database != null) return _database;
- _database = await getDatabaseUser();
- return _database;
- }
- Future<Database> getDatabaseUser() async {
- Directory directory = await getApplicationDocumentsDirectory();
- String path = join(directory.path, "Usuario.db");
- return await openDatabase(path, version: 1,
- onCreate: (Database db, int version) async {
- await db.execute("CREATE TABLE Usuario ("
- "id integer primary key,"
- "username TEXT,"
- "password TEXT,"
- "nivel TEXT"
- ")");
- });
- }
- loginLogicpar() async {
- var X = 'Roberto';
- final db = await database;
- var result = await db.rawQuery('SELECT * FROM Usuario WHERE username=?', [X]);
- return result;
- }
- loginLogic() async {
- var X = 'Roberto';
- final user = await loginLogicpar();
- if(user==X){
- return 1;
- }
- return 0;
- }
- //model usuario.dart
- class Usuario{
- int id;
- String username;
- String password;
- String nivel;
- Usuario ({this.id, this.username, this.password, this.nivel});
- //To insert the data in the bd, we need to convert it into a Map
- //Para insertar los datos en la bd, necesitamos convertirlo en un Map
- Map<String, dynamic> toMap() => {
- "id": id,
- "username": username,
- "password": password,
- "nivel": nivel,
- };
- //to receive the data we need to pass it from Map to json
- //para recibir los datos necesitamos pasarlo de Map a json
- factory Usuario.fromMap(Map<String, dynamic> json) => new Usuario(
- id: json["id"],
- username: json["username"],
- password: json["password"],
- nivel: json["nivel"],
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement