Skip to main content
 首页 » 编程设计

php之Laravel之动态读取 SQLite 文件

2024年05月06日45bjzhanghao

我正在尝试开发一个通过上传接受 SQLite 文件、保存它并解析 SQLite 数据库中的数据的项目。我发现当我使用较小的数据库文件时,不会创建“-wal”和“-shm”文件,但当使用较大的数据库(至少 2mb)时,它们会创建。

这是我正在使用的代码:

$destination = storage_path().'/tempDir/databases/'.$userId.'/'; 
 
//store the file in the directory 
$request->file('database')->move($destination .'database/database.db'); 
 
$destination = $destination.'database/database.db'; 
Config::set('database.connections.sqlite.database', $dbLocation); 
$sqliteDbConnection = \DB::connection('sqlite'); 
$getUsers = $sqliteDbConnection->table("users")->get(); 

每当我尝试执行此操作时,都会收到以下错误: “SQLSTATE[HY000]:一般错误:10 磁盘 I/O 错误(SQL:从\“用户\”中选择 *)”

有人知道为什么会这样吗?我可以确认保存到服务器的文件信誉良好,我可以使用其他 SQLite 查看器打开它。

更新:Homestead 和我的 MAC 之间的链接是否可能禁止 Laravel 访问该文件?

UPDATE2:问题100%出在VirtualBox和计算机之间的共享机制上。我通过 XAMPP 尝试过,效果很好。搞什么?

请您参考如下方法:

这就是我如何通过自定义 PDO 连接获取 Builder 实例:

$pathname = '/absolute/path/to.sqlite'; 
 
$connection = new \Illuminate\Database\SQLiteConnection(new \PDO('sqlite:' . $pathname)); 
 
$builder = new \Illuminate\Database\Query\Builder($connection); 
 
$builder->newQuery()->from('my_table')->get()->all();