【发布时间】:2019-05-02 07:56:25
【问题描述】:
您好,我正在尝试这样当我将一个新的 csvfile 添加到我的文件夹中时,它将被插入到我的数据库中,但我不知道该怎么做。 这是我的 csv 文件的 eksample https://i.gyazo.com/5f97c964c12f9f38fe4f6fbc901158a8.png
我将首先展示我的 DB.php 类,我将使用它来执行我的数据。
我知道这段代码中显示了 2 个表格,用于测试我得到正确的数据
<?php
class DB extends \PDO
{
/**
* @var
*/
private $host;
/**
* @var
*/
private $user;
/**
* @var
*/
private $pass;
/**
* @var PDO
*/
private $conn;
/**
* @var
*/
private $query;
/**
* @var bool
*/
private $debug = false;
/**
* @param bool $bool
*/
public function debug($bool = true)
{
$this->debug = $bool;
}
/**
* @param $sql
*/
private function setQuery($sql)
{
$this->query = $this->prepare($sql);
}
/**
* DB constructor.
* @param $dbhost
* @param $dbuser
* @param $dbpass
* @param array $options
*/
public function __construct($dbhost, $dbuser, $dbpass, $options = [])
{
try {
$this->conn = parent::__construct($dbhost, $dbuser, $dbpass, $options);
} catch (\PDOException $e) {
print "Fejl!: " . $e->getMessage() . "<br/>";
die();
}
}
/**
* @param string $sql
* @param bool $params
* @param int|mixed|null $returnType
* @return mixed
*/
public function query($sql, $params = false, $returnType = \PDO::FETCH_OBJ)
{
$this->setQuery($sql);
$this->execute($params);
$this->count = $this->query->rowCount();
return $this->query->fetchAll($returnType);
}
/**
* @param $params
*/
private function execute($params)
{
if($params){
$this->query->execute($params);
} else {
$this->query->execute();
}
if($this->debug){
echo '<pre id="debug_params">',$this->query->debugDumpParams(),'</pre>';
}
}
/**
* @param $sql
* @param bool $params
* @return mixed
*/
public function single($sql, $params = false){
$data = $this->query($sql, $params);
if(sizeof($data) === 1){
return $data[0];
} else {
return false;
}
}
/**
* @param $sql
* @param bool $params
* @return mixed
*/
public function first($sql, $params = false){
return $this->query($sql, $params)[0];
}
/**
* @param $sql
* @param bool $params
* @return mixed
*/
public function last($sql, $params = false){
return $this->query($sql, $params)[$this->count - 1];
}
/**
* @param $sql
* @param bool $params
* @return mixed
*/
public function toList($sql, $params = false){
return $this->query($sql, $params);
}
/**
* @param $sql
* @param bool $params
* @return mixed
*/
public function lastId($sql, $params = false){
$this->query($sql, $params);
return $this->lastInsertId();
}
}
这是我的csvtable.php我的索引文件,这是用户将看到的。
<?php
if ($user->is_loggedin() == true) {
// File selector
$path = "./assets/csv";
$latest_ctime = 0;
$latest_filename = '';
$d = dir($path);
while (false !== ($entry = $d->read())) {
$filepath = "{$path}/{$entry}";
// could do also other checks than just checking whether the entry is a file
if (is_file($filepath) && filectime($filepath) > $latest_ctime){
$latest_ctime = filectime($filepath);
$latest_filename = $entry;
}
}
// File selector end
echo'<h1 class="text-center">CSV Table</h1>';
echo'<h6 class="text-center">'.$latest_filename.'</h6>';
echo '<table class=" table table-striped table-bordered table-hover" style="width:100%">';
echo'<tbody>';
$f = fopen("$path/$latest_filename", "r");
while (($row = fgetcsv($f, 1000, ";")) !== false) {
//Create table using the filename as tablename, if table already exist insert data.
$csv->createCsvTable($row);
echo '<tr>';
foreach ($row as $cell) {
echo '<td>' .htmlspecialchars($cell, ENT_COMPAT). '</td>';
}
echo '</tr>';
}
fclose($f);
echo'</tbody>';
echo '</table>';
//csv table end
?><table id="example" class="table-hover table table-striped table-bordered" style="width:100%">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Datum</th>
<th scope="col">Property</th>
<th scope="col">Criterion</th>
<th scope="col">Type</th>
<th scope="col">Nominal</th>
<th scope="col">Actual</th>
<th scope="col">Tol-</th>
<th scope="col">Tol+</th>
<th scope="col">Dev</th>
</tr>
</thead>
<tbody>
<?php foreach($csv->getCsv() as $csv) { ?>
<tr>
<td><?= $csv->Name; ?></td>
<td><?= $csv->Datum; ?></td>
<td><?= $csv->Property; ?></td>
<td><?= $csv->Criterion; ?></td>
<td><?= $csv->Type; ?></td>
<td><?= $csv->Nominal; ?></td>
<td><?= $csv->Actual; ?></td>
<td><?= $csv->Tolminus; ?></td>
<td><?= $csv->Tolplus; ?></td>
<td><?= $csv->Dev; ?></td>
</tr>
<?php }?>
</tbody>
</table>
<?php }
这是我的 CSV 类,我不知道如何编写 createCsvTable 我想要它做的是将 csv 文件数据插入到我创建的表中。
<?php
class Csv extends \PDO {
private $db = null;
/**
* Settings constructor.
*/
public function __construct($db)
{
$this->db = $db;
}
/**
* Menu & Admin Menu
* @return mixed
*/
public function getCsv()
{
return $this->db->toList("SELECT * FROM Test");
}
public function createCsvTable()
{
return $this->db->toList("CREATE TABLE IF NOT EXISTS Test(
id int (11),
Name VARCHAR(30),
Datum VARCHAR(30),
Property VARCHAR(30),
Criterion VARCHAR(30),
Type VARCHAR(30),
Nominal DECIMAL(10,2),
Actual DECIMAL(10,2),
Tolminus DECIMAL(10,2),
Tolplus DECIMAL(10,2),
Dev DECIMAL(10,2)
);
/* INSERT QUERY */
INSERT INTO Testeses(
id, Name, Datum, Property, Criterion, Type,
Nominal, Actual, Tolminus, Tolplus, Dev
)
VALUES
(
?,?,?,?,?,?,
?,?,?,?,?
);");
}
}
我知道如果我更换了 ?它会插入一些实际数据,但我需要它来自 csv 文件。这是主要问题
所以为了清楚起见,我的问题是“如何将我的 csv 文件中的数据插入到我的数据库中?”
如果您知道如何以文件名命名表格,我将不胜感激。
【问题讨论】:
-
好吧,但我不需要将文件名存储在我的数据库中我需要将文件作为我的数据库中的表
-
你试过什么?确切的问题是什么?编写一个从文件中读取数据并运行正确查询的循环并不难
-
正如@NicoHaase 所说。循环遍历 csv 文件,一一读取行并将其插入到您的数据库中。
-
我试图做的是,起初我只想能够在我的网站上看到该文件,但由于我现在需要使用它,所以我需要将它存储在我的数据库中,所以我可以使用它的数据。但我不知道该怎么做。