funcion base de datos y registro

This commit is contained in:
25030248hasel
2026-05-23 06:03:29 -06:00
parent 6638f471e1
commit 1709a51ecc
5 changed files with 250 additions and 111 deletions

View File

@@ -0,0 +1,38 @@
import 'package:mysql_client/mysql_client.dart';
class MySqlService {
MySQLConnection? _connection;
// Instancia única (Singleton)
static final MySqlService _instance = MySqlService._internal();
factory MySqlService() => _instance;
MySqlService._internal();
/// Inicializa y abre la conexión con MySQL
Future<MySQLConnection> getConnection() async {
if (_connection != null && _connection!.connected) {
return _connection!;
}
// ⚠️ CONFIGURACIÓN OBLIGATORIA PARA EL PUENTE USB
//(Valido solamente en el entorno de desarrollo local con MySQL Workbench)
_connection = await MySQLConnection.createConnection(
host: '127.0.0.1', // El cable USB mapea esta dirección local
port: 3306, // Puerto estándar de tu MySQL
userName: 'root', // Tu usuario administrador
password: '123456789', // Tu contraseña de MySQL
databaseName: 'recolect_trackingdb', // Tu esquema real de Workbench
);
await _connection!.connect();
return _connection!;
}
/// Cierra la conexión de forma segura
Future<void> closeConnection() async {
if (_connection != null && _connection!.connected) {
await _connection!.close();
_connection = null;
}
}
}