Mysql 通过读取 txt 文件更新 table?
Mysql update table by reading txt file?
/var/lib/mysql-files/myfile.txt 文件包含
23/08/2020 mycluster1 192.168.0.10 1515G 22G 1493G 2 15 17
我想通过阅读 /var/lib/mysql-files/myfile.txt 更新 servercheck table 我尝试了很多组合但找不到合适的解决方案我也搜索导入命令但找不到更新
mysqlimport -u myuser -pxxXxX1xF databasename UPDATE 'servercheck' (`id`, `date`, `cluster`, `ip`, `totalsize`, `used`, `available`, `-10MBfiles`, `+10MBfiles`, `totalfiles`) VALUES LOAD_FILE('/var/lib/mysql-files/myfile.txt') Where ip='192.168.0.10';
如何更新此 table?
谢谢堆
一定要继续尝试语法的随机组合。您快到了!你只有 25,622,702,110,040,813,336,081,912,836,183,802,580,535,642,835,740,419,266,502,085,577,211,882 ,272,650,414,517,527,799,478,743,488,400,747,943,069,000,615,135,250,963,344,833,331,485,320,251,677,534,098,797,378,129,631,890,777,593,821,450,771,100,601,051,040,658,154,732,825,998,434,179,861,242,822,823,294,569,561,935,370,384,214,295,397,821,780,452,001,056,445,606,246,731,902,062,784,801,234,354,567,343,837,156,149,230,451,302,848,181,975,698,028,988,742,337,466,917,653,761,882,052,114,517,822,309,817,669,841,068,938,391,156,712,267,414,065,202,136,112,693,807,721,164,567,033,575,479,857,283,752,212,725,180,807,950,664,529,926,143,586,635,589,599,609,374 more combinations to try.
(即 95251,长度为 251 的字符串数,由 95 个 printable ASCII 字符组成。对于上面显示的示例,减去 1。 )
嗯。当我们这样想时,也许“尝试每一种组合”并不是编写代码的最佳方式。
阅读the documentation for the mysqlimport tool。它不支持自定义更新语句。
您可以将文本数据行导入 table。一行文本数据对应于 table.
中的一行
默认情况下,该行作为新行插入。您还可以使用 --replace
选项,这样文本行 将替换 现有行,如果文本中的值与您的 table.
这是覆盖数据的唯一可能性。您的 table 中必须有一个主键或唯一键,并且该主键或唯一键中的值必须与您导入的文本文件的相应列中的值冲突。否则,MySQL 无法知道要替换哪一行。
如果你想用其他方式匹配行(即不是主键或唯一键),或者你想覆盖一些列而不是其他列,那么你应该将文本数据导入 new table,然后您可以执行任何 SQL 查询,将新导入的行与原始 table 中的行匹配,并覆盖一些列。
另一种解决方案是忘记使用 mysqlimport
,而是用您喜欢的语言编写脚本(为此我会使用 python,但任何语言都可以),请阅读逐行文本文件,并逐行发出 SQL 语句以更改 table 中的行。您可以使用 UPDATE,也可以使用 INSERT...ON DUPLICATE KEY UPDATE,具体取决于您要执行的操作。
这是脚本,如果有人需要的话。
srvstat.txt
a=Australia b=04/09/2020 15:40 c=strg1-au.myserver.com d=192.168.0.15 e=/etc/mydata/ f=147G g=1G h=1% i= 147G j=99% k=0 l=0 m=0 n=OK o=OK p=32547320 KB r=27402 MB s=500G t=494G
并且此 php 代码读取并更新 table。
<?php
$input = "/etc/system/srvstats/storage/srvstats.txt";
$dbHost = "localhost";
$dbUser = "myuser";
$dbPass = "xxxxxxxxxxF";
$db = "admin_my2";
$table = "storagecheck";
$id = "2";
$data = explode('=', file_get_contents($input));
$location = trim(substr($data[1], 0, -2));
$date = trim(substr($data[2], 0, -2));
$cluster = trim(substr($data[3], 0, -2));
$ip = trim(substr($data[4], 0, -2));
$mounted = trim(substr($data[5], 0, -2));
$totalsize = trim(substr($data[6], 0, -2));
$used = trim(substr($data[7], 0, -2));
$usedPercent = trim(substr($data[8], 0, -2));
$free = trim(substr($data[9], 0, -2));
$freePercent = trim(substr($data[10], 0, -2));
$minus50MBFiles = trim(substr($data[11], 0, -2));
$plus50MBFiles = trim(substr($data[12], 0, -2));
$totalFiles = trim(substr($data[13], 0, -2));
$pinggateway = trim(substr($data[14], 0, -2));
$raidhealth = trim(substr($data[15], 0, -2));
$memorysize = trim(substr($data[16], 0, -2));
$freememory = trim(substr($data[17], 0, -2));
$backupsize = trim(substr($data[18], 0, -2));
$backupspaceremaining = trim($data[19]);
$conn = new mysqli($dbHost, $dbUser, $dbPass, $db);
!$conn->connect_error OR die("Connection failed: " . $conn->connect_error);
$sql = "UPDATE `$table` SET `location` = '$location', `date` = '$date', `cluster` = '$cluster', `ip` = '$ip', `mounted` = '$mounted', `totalsize` = '$totalsize', `used` = '$used', `used%` = '$usedPercent', `available` = '$free' , `free%` = '$freePercent', `-50MBfiles` = '$minus50MBFiles',`+50MBfiles` = '$plus50MBFiles',`totalfiles` = '$totalFiles',`pinggateway` = '$pinggateway', `raidhealth` = '$raidhealth', `memorysize` = '$memorysize', `freememory` = '$freememory', `backupsize` = '$backupsize', `backupspaceremaining%` = '$backupspaceremaining' WHERE `id` = $id";
$conn->query($sql);
echo "Entry ID $id updated succesfully.";
/var/lib/mysql-files/myfile.txt 文件包含
23/08/2020 mycluster1 192.168.0.10 1515G 22G 1493G 2 15 17
我想通过阅读 /var/lib/mysql-files/myfile.txt 更新 servercheck table 我尝试了很多组合但找不到合适的解决方案我也搜索导入命令但找不到更新
mysqlimport -u myuser -pxxXxX1xF databasename UPDATE 'servercheck' (`id`, `date`, `cluster`, `ip`, `totalsize`, `used`, `available`, `-10MBfiles`, `+10MBfiles`, `totalfiles`) VALUES LOAD_FILE('/var/lib/mysql-files/myfile.txt') Where ip='192.168.0.10';
如何更新此 table?
谢谢堆
一定要继续尝试语法的随机组合。您快到了!你只有 25,622,702,110,040,813,336,081,912,836,183,802,580,535,642,835,740,419,266,502,085,577,211,882 ,272,650,414,517,527,799,478,743,488,400,747,943,069,000,615,135,250,963,344,833,331,485,320,251,677,534,098,797,378,129,631,890,777,593,821,450,771,100,601,051,040,658,154,732,825,998,434,179,861,242,822,823,294,569,561,935,370,384,214,295,397,821,780,452,001,056,445,606,246,731,902,062,784,801,234,354,567,343,837,156,149,230,451,302,848,181,975,698,028,988,742,337,466,917,653,761,882,052,114,517,822,309,817,669,841,068,938,391,156,712,267,414,065,202,136,112,693,807,721,164,567,033,575,479,857,283,752,212,725,180,807,950,664,529,926,143,586,635,589,599,609,374 more combinations to try.
(即 95251,长度为 251 的字符串数,由 95 个 printable ASCII 字符组成。对于上面显示的示例,减去 1。 )
嗯。当我们这样想时,也许“尝试每一种组合”并不是编写代码的最佳方式。
阅读the documentation for the mysqlimport tool。它不支持自定义更新语句。
您可以将文本数据行导入 table。一行文本数据对应于 table.
中的一行默认情况下,该行作为新行插入。您还可以使用 --replace
选项,这样文本行 将替换 现有行,如果文本中的值与您的 table.
这是覆盖数据的唯一可能性。您的 table 中必须有一个主键或唯一键,并且该主键或唯一键中的值必须与您导入的文本文件的相应列中的值冲突。否则,MySQL 无法知道要替换哪一行。
如果你想用其他方式匹配行(即不是主键或唯一键),或者你想覆盖一些列而不是其他列,那么你应该将文本数据导入 new table,然后您可以执行任何 SQL 查询,将新导入的行与原始 table 中的行匹配,并覆盖一些列。
另一种解决方案是忘记使用 mysqlimport
,而是用您喜欢的语言编写脚本(为此我会使用 python,但任何语言都可以),请阅读逐行文本文件,并逐行发出 SQL 语句以更改 table 中的行。您可以使用 UPDATE,也可以使用 INSERT...ON DUPLICATE KEY UPDATE,具体取决于您要执行的操作。
这是脚本,如果有人需要的话。
srvstat.txt
a=Australia b=04/09/2020 15:40 c=strg1-au.myserver.com d=192.168.0.15 e=/etc/mydata/ f=147G g=1G h=1% i= 147G j=99% k=0 l=0 m=0 n=OK o=OK p=32547320 KB r=27402 MB s=500G t=494G
并且此 php 代码读取并更新 table。
<?php
$input = "/etc/system/srvstats/storage/srvstats.txt";
$dbHost = "localhost";
$dbUser = "myuser";
$dbPass = "xxxxxxxxxxF";
$db = "admin_my2";
$table = "storagecheck";
$id = "2";
$data = explode('=', file_get_contents($input));
$location = trim(substr($data[1], 0, -2));
$date = trim(substr($data[2], 0, -2));
$cluster = trim(substr($data[3], 0, -2));
$ip = trim(substr($data[4], 0, -2));
$mounted = trim(substr($data[5], 0, -2));
$totalsize = trim(substr($data[6], 0, -2));
$used = trim(substr($data[7], 0, -2));
$usedPercent = trim(substr($data[8], 0, -2));
$free = trim(substr($data[9], 0, -2));
$freePercent = trim(substr($data[10], 0, -2));
$minus50MBFiles = trim(substr($data[11], 0, -2));
$plus50MBFiles = trim(substr($data[12], 0, -2));
$totalFiles = trim(substr($data[13], 0, -2));
$pinggateway = trim(substr($data[14], 0, -2));
$raidhealth = trim(substr($data[15], 0, -2));
$memorysize = trim(substr($data[16], 0, -2));
$freememory = trim(substr($data[17], 0, -2));
$backupsize = trim(substr($data[18], 0, -2));
$backupspaceremaining = trim($data[19]);
$conn = new mysqli($dbHost, $dbUser, $dbPass, $db);
!$conn->connect_error OR die("Connection failed: " . $conn->connect_error);
$sql = "UPDATE `$table` SET `location` = '$location', `date` = '$date', `cluster` = '$cluster', `ip` = '$ip', `mounted` = '$mounted', `totalsize` = '$totalsize', `used` = '$used', `used%` = '$usedPercent', `available` = '$free' , `free%` = '$freePercent', `-50MBfiles` = '$minus50MBFiles',`+50MBfiles` = '$plus50MBFiles',`totalfiles` = '$totalFiles',`pinggateway` = '$pinggateway', `raidhealth` = '$raidhealth', `memorysize` = '$memorysize', `freememory` = '$freememory', `backupsize` = '$backupsize', `backupspaceremaining%` = '$backupspaceremaining' WHERE `id` = $id";
$conn->query($sql);
echo "Entry ID $id updated succesfully.";