Skip to content

Instantly share code, notes, and snippets.

@fgonga
Created April 14, 2023 06:45
Show Gist options
  • Save fgonga/3033dc3ef2532575d4c9926a13c06614 to your computer and use it in GitHub Desktop.
Save fgonga/3033dc3ef2532575d4c9926a13c06614 to your computer and use it in GitHub Desktop.
Flutter Database Helper
import 'dart:async';
import 'dart:io';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path_provider/path_provider.dart';
class DatabaseHelper {
static const _databaseName = 'database.db';
static const _databaseVersion = 1;
// torna esta classe singleton
DatabaseHelper._privateConstructor();
static final DatabaseHelper instance = DatabaseHelper._privateConstructor();
// tem apenas uma referência ao banco de dados
static Database? _database;
Future<Database> get database async {
if (_database != null ) return _database!;
// instancia o banco de dados na primeira vez que ele for acessado
_database = await _initDatabase();
return _database!;
}
// abre o banco de dados
_initDatabase() async {
print("A criar banco de dados");
Directory documentsDirectory = await getApplicationDocumentsDirectory();
String path = join(documentsDirectory.path, _databaseName);
print(path);
return await openDatabase(path,
version: _databaseVersion, onCreate: _onCreate);
}
// cria a tabela no banco de dados
Future _onCreate(Database db, int version) async {
await db.execute('''
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!50503 SET NAMES utf8mb4 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
DROP TABLE IF EXISTS `cliente`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `cliente` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`nome` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`endereco` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`telefone` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
LOCK TABLES `cliente` WRITE;
/*!40000 ALTER TABLE `cliente` DISABLE KEYS */;
/*!40000 ALTER TABLE `cliente` ENABLE KEYS */;
UNLOCK TABLES;
DROP TABLE IF EXISTS `compra`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `compra` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`data` datetime DEFAULT NULL,
`totalQtd` int DEFAULT NULL,
`totalPagar` double(8,2) DEFAULT '0.00',
`totalPago` double(8,2) DEFAULT '0.00',
`troco` double(8,2) DEFAULT '0.00',
`fornecedorId` bigint unsigned NOT NULL,
`usuarioId` bigint unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `compra_fornecedorId_foreign` (`fornecedorId`),
KEY `compra_usuarioId_foreign` (`usuarioId`),
CONSTRAINT `compra_fornecedorId_foreign` FOREIGN KEY (`fornecedorId`) REFERENCES `fornecedor` (`id`),
CONSTRAINT `compra_usuarioId_foreign` FOREIGN KEY (`usuarioId`) REFERENCES `usuario` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
LOCK TABLES `compra` WRITE;
/*!40000 ALTER TABLE `compra` DISABLE KEYS */;
/*!40000 ALTER TABLE `compra` ENABLE KEYS */;
UNLOCK TABLES;
DROP TABLE IF EXISTS `empresa`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `empresa` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`endereco` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`telefone` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`nif` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`nome` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`logo` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
LOCK TABLES `empresa` WRITE;
/*!40000 ALTER TABLE `empresa` DISABLE KEYS */;
/*!40000 ALTER TABLE `empresa` ENABLE KEYS */;
UNLOCK TABLES;
DROP TABLE IF EXISTS `fornecedor`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `fornecedor` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`nome` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`endereco` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`telefone` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`nif` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
LOCK TABLES `fornecedor` WRITE;
/*!40000 ALTER TABLE `fornecedor` DISABLE KEYS */;
/*!40000 ALTER TABLE `fornecedor` ENABLE KEYS */;
UNLOCK TABLES;
DROP TABLE IF EXISTS `movimentoDeStock`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `movimentoDeStock` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`tipo` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`ref` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`qtd` int DEFAULT '0',
`usuarioId` bigint unsigned NOT NULL,
`produtoId` bigint unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `movimentodestock_usuarioId_foreign` (`usuarioId`),
KEY `movimentodestock_produtoId_foreign` (`produtoId`),
CONSTRAINT `movimentodestock_produtoId_foreign` FOREIGN KEY (`produtoId`) REFERENCES `produto` (`id`),
CONSTRAINT `movimentodestock_usuarioId_foreign` FOREIGN KEY (`usuarioId`) REFERENCES `usuario` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
LOCK TABLES `movimentoDeStock` WRITE;
/*!40000 ALTER TABLE `movimentoDeStock` DISABLE KEYS */;
/*!40000 ALTER TABLE `movimentoDeStock` ENABLE KEYS */;
UNLOCK TABLES;
DROP TABLE IF EXISTS `produto`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `produto` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`nome` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`preco` double(8,2) DEFAULT '0.00',
`foto` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`stock` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
LOCK TABLES `produto` WRITE;
/*!40000 ALTER TABLE `produto` DISABLE KEYS */;
/*!40000 ALTER TABLE `produto` ENABLE KEYS */;
UNLOCK TABLES;
DROP TABLE IF EXISTS `produtoNaCompra`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `produtoNaCompra` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`nome` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`qtd` int DEFAULT '0',
`preco` double(8,2) DEFAULT '0.00',
`precoTotal` double(8,2) DEFAULT '0.00',
`produtoId` bigint unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `produtonacompra_produtoId_foreign` (`produtoId`),
CONSTRAINT `produtonacompra_produtoId_foreign` FOREIGN KEY (`produtoId`) REFERENCES `produto` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
LOCK TABLES `produtoNaCompra` WRITE;
/*!40000 ALTER TABLE `produtoNaCompra` DISABLE KEYS */;
/*!40000 ALTER TABLE `produtoNaCompra` ENABLE KEYS */;
UNLOCK TABLES;
DROP TABLE IF EXISTS `produtoNaVenda`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `produtoNaVenda` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`nome` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`qtd` int DEFAULT '0',
`preco` double(8,2) DEFAULT '0.00',
`precoTotal` double(8,2) DEFAULT '0.00',
`produtoId` bigint unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `produtonavenda_produtoId_foreign` (`produtoId`),
CONSTRAINT `produtonavenda_produtoId_foreign` FOREIGN KEY (`produtoId`) REFERENCES `produto` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
LOCK TABLES `produtoNaVenda` WRITE;
/*!40000 ALTER TABLE `produtoNaVenda` DISABLE KEYS */;
/*!40000 ALTER TABLE `produtoNaVenda` ENABLE KEYS */;
UNLOCK TABLES;
DROP TABLE IF EXISTS `sistema`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `sistema` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`nome` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`tipo` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
LOCK TABLES `sistema` WRITE;
/*!40000 ALTER TABLE `sistema` DISABLE KEYS */;
/*!40000 ALTER TABLE `sistema` ENABLE KEYS */;
UNLOCK TABLES;
DROP TABLE IF EXISTS `usuario`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `usuario` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`nome` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`tipo` char(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`pin` smallint DEFAULT NULL,
`ativo` tinyint(1) DEFAULT '1',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
LOCK TABLES `usuario` WRITE;
/*!40000 ALTER TABLE `usuario` DISABLE KEYS */;
INSERT INTO `usuario` VALUES (1,'root','a',1234,1);
/*!40000 ALTER TABLE `usuario` ENABLE KEYS */;
UNLOCK TABLES;
DROP TABLE IF EXISTS `venda`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `venda` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`data` datetime DEFAULT NULL,
`totalQtd` int DEFAULT NULL,
`totalPagar` double(8,2) DEFAULT '0.00',
`totalPago` double(8,2) DEFAULT '0.00',
`troco` double(8,2) DEFAULT '0.00',
`clienteId` bigint unsigned NOT NULL,
`usuarioId` bigint unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `venda_clienteId_foreign` (`clienteId`),
KEY `venda_usuarioId_foreign` (`usuarioId`),
CONSTRAINT `venda_clienteId_foreign` FOREIGN KEY (`clienteId`) REFERENCES `cliente` (`id`),
CONSTRAINT `venda_usuarioId_foreign` FOREIGN KEY (`usuarioId`) REFERENCES `usuario` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
LOCK TABLES `venda` WRITE;
/*!40000 ALTER TABLE `venda` DISABLE KEYS */;
/*!40000 ALTER TABLE `venda` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
''');
}
// insere um registro no banco de dados
Future<int> insert(String table, Map<String, dynamic> row) async {
Database db = await instance.database;
return await db.insert(table, row);
}
// atualiza um registro no banco de dados
Future<int> update(String table, Map<String, dynamic> row) async {
Database db = await instance.database;
int id = row['id'];
return await db.update(table, row, where: 'id = ?', whereArgs: [id]);
}
// exclui um registro do banco de dados
Future<int> delete(String table, int id) async {
Database db = await instance.database;
return await db.delete(table, where: 'id = ?', whereArgs: [id]);
}
// retorna todos os registros do banco de dados
Future<List<Map<String, dynamic>>> queryAllRows(String table) async {
Database db = await instance.database;
return await db.query(table);
}
// retorna um registro pelo id
Future<Map<String, dynamic>> queryById(table, int id) async {
Database db = await instance.database;
List<Map<String, dynamic>> result =
await db.query(table, where: 'id = ?', whereArgs: [id], limit: 1);
return result.first;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment