feat: 重构数据库配置并支持多数据库类型
- 将数据库配置从硬编码改为从外部文件加载 - 支持MySQL 5.7+和SQLite两种数据库类型 - 改进安装向导流程,增加数据库类型选择 - 更新README文档说明数据库要求 - 替换CDN链接为国内镜像源 - 增加安装完成后的安全提醒 重构数据库类以支持更灵活的配置方式,包括端口设置和MySQL 5.7兼容性处理。安装向导现在可以正确配置SQLite或MySQL数据库,并生成相应的配置文件。同时优化了前端资源加载速度。
This commit is contained in:
@@ -5,12 +5,25 @@
|
||||
*/
|
||||
|
||||
class Database {
|
||||
private $host = 'localhost';
|
||||
private $db_name = 'submission_system';
|
||||
private $username = 'root';
|
||||
private $password = '';
|
||||
private $host;
|
||||
private $port;
|
||||
private $db_name;
|
||||
private $username;
|
||||
private $password;
|
||||
private $use_sqlite;
|
||||
private $is_mysql_57;
|
||||
private $conn;
|
||||
private $use_sqlite = false; // 设置为true使用SQLite
|
||||
|
||||
public function __construct() {
|
||||
$db_config = @include __DIR__ . '/db_config.php';
|
||||
$this->host = $db_config['host'] ?? 'localhost';
|
||||
$this->port = $db_config['port'] ?? '3306';
|
||||
$this->db_name = $db_config['db_name'] ?? 'submission_system';
|
||||
$this->username = $db_config['username'] ?? 'root';
|
||||
$this->password = $db_config['password'] ?? '';
|
||||
$this->use_sqlite = $db_config['use_sqlite'] ?? false;
|
||||
$this->is_mysql_57 = $db_config['is_mysql_57'] ?? false;
|
||||
}
|
||||
|
||||
public function getConnection() {
|
||||
$this->conn = null;
|
||||
@@ -18,22 +31,20 @@ class Database {
|
||||
try {
|
||||
if ($this->use_sqlite) {
|
||||
// SQLite配置
|
||||
$this->conn = new PDO('sqlite:' . __DIR__ . '/../data/database.sqlite');
|
||||
$this->conn = new PDO('sqlite:' . __DIR__ . '/../data/submission.db');
|
||||
} else {
|
||||
// MySQL配置
|
||||
$this->conn = new PDO(
|
||||
"mysql:host=" . $this->host . ";dbname=" . $this->db_name . ";charset=utf8mb4",
|
||||
$this->username,
|
||||
$this->password
|
||||
);
|
||||
$dsn = "mysql:host=" . $this->host . ";port=" . $this->port . ";dbname=" . $this->db_name . ";charset=utf8mb4";
|
||||
$this->conn = new PDO($dsn, $this->username, $this->password, [
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
||||
PDO::ATTR_EMULATE_PREPARES => false,
|
||||
]);
|
||||
}
|
||||
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
$this->conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
|
||||
} catch(PDOException $exception) {
|
||||
echo "连接失败: " . $exception->getMessage();
|
||||
return $this->conn;
|
||||
} catch(PDOException $e) {
|
||||
throw new Exception("数据库连接失败: " . $e->getMessage());
|
||||
}
|
||||
|
||||
return $this->conn;
|
||||
}
|
||||
|
||||
public function initDatabase() {
|
||||
@@ -143,5 +154,9 @@ class Database {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function isMySQL57() {
|
||||
return $this->is_mysql_57;
|
||||
}
|
||||
}
|
||||
?>
|
Reference in New Issue
Block a user