如何从.txt文件中导出多个数据并插入数据库中

huangapple go评论67阅读模式
英文:

How to export multiply data from a .txt and insert into a database

问题

I can provide a translation of your text as requested:

如何从不同的文本文件中导出多个数据并将数据导入数据库?

所以我有一个名为:admin_cars 的数据库和一个表格:brands;如下所示:

CREATE TABLE `brands` (
  `id` int(11) NOT NULL,
  `bmw` varchar(500) NOT NULL,
  `mercedes` varchar(500) NOT NULL,
  `audi` varchar(500) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

现在我想从:/var/www/html/bmw/index.txt 导出数据到 -> bmw

我已经在 .PHP 中编写了脚本,它运行正常。以下是代码:

<?php

$dsn = 'mysql:dbname=admin_cars;host=127.0.0.1';
$user = 'admin_cars';
$password = '********';

try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo '连接失败: ' . $e->getMessage();
}

$file = new SplFileObject('/var/www/html/bmw/index.txt');
while (!$file->eof()) {
    $line = $file->fgets();
    list($bmw) = explode(';', $line);

    $sth = $dbh->prepare('INSERT INTO brands values(NULL,?)');
    $sth->bindValue(1, $bmw, PDO::PARAM_STR);
    $sth->execute();
}
?>

但现在我还想从 /var/www/html/mercedes/index.txt 导出数据到 -> mercedes,以及从 /var/www/html/audi/index.txt 导出数据到 -> audi

我尝试了以下代码:

<?php

$dsn = 'mysql:dbname=admin_cars;host=127.0.0.1';
$user = 'admin_cars';
$password = '********';

try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo '连接失败: ' . $e->getMessage();
}

$file = new SplFileObject('/var/www/html/bmw/index.txt');
while (!$file->eof())

$file = new SplFileObject('/var/www/html/mercedes/index.txt');
while (!$file->eof())

$file = new SplFileObject('/var/www/html/audi/index.txt');
while (!$file->eof()) {
    $line = $file->fgets();
    list($bmw) = explode(';', $line);

    $line = $file->fgets();
    list($mercedes) = explode(';', $line);

    $line = $file->fgets();
    list($audi) = explode(';', $line);

    $sth = $dbh->prepare('INSERT INTO brands values(NULL,?,?,?)');
    $sth->bindValue(1, $bmw, PDO::PARAM_STR);
    $sth->bindValue(2, $mercedes, PDO::PARAM_STR);
    $sth->bindValue(3, $audi, PDO::PARAM_STR);
    $sth->execute();
}
?>

但当我运行这个 .PHP 脚本时,我收到错误消息并出现故障。什么是将多个数据导出到数据库表的最佳解决方案?

非常感谢您的时间和帮助!

英文:

How can i export multiply data from different text files and import the data into a database?

So i have a database called: admin_cars and a table: brands ; like:

CREATE TABLE `brands` (
  `id` int(11) NOT NULL,
  `bmw` varchar(500) NOT NULL,
  `mercedes` varchar(500) NOT NULL
  `audi` varchar(500) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Now i want to export the data from: /var/www/html/bmw/index.txt into -> bmw

I have already written the script in .PHP and it works fine. This is the code:

&lt;?php

$dsn = &#39;mysql:dbname=admin_cars;host=127.0.0.1&#39;;
$user = &#39;admin_cars&#39;;
$password = &#39;********&#39;;
 
try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo &#39;Connection failed: &#39; . $e-&gt;getMessage();
}
 
$file= new SplFileObject(&#39;/var/www/html/bmw/index.txt&#39;);
while(!$file-&gt;eof())
{
    $line=$file-&gt;fgets();
    list($bmw)=explode(&#39;;&#39;,$line);
    
    $sth = $dbh-&gt;prepare(&#39;INSERT INTO brands values(NULL,?)&#39;);
    $sth-&gt;bindValue(1,$bmw,PDO::PARAM_STR);
    $sth-&gt;execute();   
}
 
?&gt;

But now i want to export also the data from /var/www/html/mercedes/index.txt into -> mercedes and /var/www/html/audi/index.txt into -> audi

I tried this:

&lt;?php

$dsn = &#39;mysql:dbname=admin_cars;host=127.0.0.1&#39;;
$user = &#39;admin_cars&#39;;
$password = &#39;********&#39;;
 
try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo &#39;Connection failed: &#39; . $e-&gt;getMessage();
}
 
$file= new SplFileObject(&#39;/var/www/html/bmw/index.txt&#39;);
while(!$file-&gt;eof())

$file= new SplFileObject(&#39;/var/www/html/mercedes/index.txt&#39;);
while(!$file-&gt;eof())

$file= new SplFileObject(&#39;/var/www/html/audi/index.txt&#39;);
while(!$file-&gt;eof())

{
    $line=$file-&gt;fgets();
    list($bmw)=explode(&#39;;&#39;,$line);

    $line=$file-&gt;fgets();
    list($mercedes)=explode(&#39;;&#39;,$line);

    $line=$file-&gt;fgets();
    list($audi)=explode(&#39;;&#39;,$line);
    
    $sth = $dbh-&gt;prepare(&#39;INSERT INTO brands values(NULL,?,?,?)&#39;);
    $sth-&gt;bindValue(1,$bmw,PDO::PARAM_STR);
    $sth-&gt;bindValue(2,$mercedes,PDO::PARAM_STR);
    $sth-&gt;bindValue(3,$audi,PDO::PARAM_STR);
    $sth-&gt;execute();   
}
 
?&gt;

But i get a error message and faults when i run the .PHP script.
What is the best solution to export multiply data into the tables of the database?

Thank you very much for your time and help!

答案1

得分: 0

You're using same variable for all files data repeatedly, so maybe that is an issue. For start, try reading them separately and write them to DB with one while loop after another.

您多次重复使用相同的变量来处理所有文件数据,这可能是一个问题。首先尝试分别读取它们,然后使用一个while循环写入数据库。

$file_bmw= new SplFileObject('/var/www/html/bmw/index.txt');
while(!$file_bmw->eof())
{
    // 输入到数据库
}
$file_mercedes= new SplFileObject('/var/www/html/mercedes/index.txt');
while(!$file_mercedes->eof())
{
    // 输入到数据库
}

What is the error displayed ?

请问显示了什么错误?

英文:

You're using same variable for all files data repeatedly, so maybe that is an issue. For start, try reading them separately and write them to DB with one while loop after another.

$file_bmw= new SplFileObject(&#39;/var/www/html/bmw/index.txt&#39;);
while(!$file_bmw-&gt;eof())
{
// input to db
}
$file_mercedes= new SplFileObject(&#39;/var/www/html/mercedes/index.txt&#39;);
while(!$file_mercedes-&gt;eof())
{
// input to db
}

What is the error displayed ?

huangapple
  • 本文由 发表于 2023年3月7日 03:48:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75655213.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定