funcion base de datos y registro
This commit is contained in:
38
lib/core/network/mysql_service.dart
Normal file
38
lib/core/network/mysql_service.dart
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user