Init: MVP de Gestión de Pagos (Stitch UI + SQLite/PHP)
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
// pagos/api/db.php
|
||||
|
||||
$db_file = __DIR__ . '/../db/finanzas.sqlite';
|
||||
$schema_file = __DIR__ . '/../db/schema.sql';
|
||||
|
||||
// Función para obtener conexión PDO a SQLite
|
||||
function getDB() {
|
||||
global $db_file, $schema_file;
|
||||
$is_new = !file_exists($db_file);
|
||||
|
||||
try {
|
||||
$pdo = new PDO("sqlite:" . $db_file);
|
||||
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
|
||||
// Si no existía, crear la BD desde schema y establecer permisos
|
||||
if ($is_new && file_exists($schema_file)) {
|
||||
$schema = file_get_contents($schema_file);
|
||||
$pdo->exec($schema);
|
||||
// Intentar dar permisos para www-data en el servidor
|
||||
@chmod($db_file, 0666);
|
||||
@chmod(dirname($db_file), 0777);
|
||||
}
|
||||
|
||||
return $pdo;
|
||||
} catch (PDOException $e) {
|
||||
http_response_code(500);
|
||||
die(json_encode(['error' => 'Connection failed: ' . $e->getMessage()]));
|
||||
}
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user