* 安装mysqlworkbench
* 新建查询sql
select * from W_SalesPackages order by guid desc limit 1000;
Copy Row (with names)
* 粘贴到 W_SalesPackages.txt文件中
# guid, pids, name, coursecontent, standard, teacherids, grades, pricedescription, packageimage, servicecontent, improvecontent, opentime, closetime, ispubli\
sh, publishdate, isvalid, inserttime, type, tutortimes, subject
'p000', 'p12345', '测试', '<p>文科:包含高二数学、英语上下学期同步复习课程、高二语文同步复习课程、高中地理同步复习课程一、高中地理同步复习课程二,共7个课程,优惠价1500元(按单科报名原价:2100元)!赠送课程:加赠高一英语上下学期同步复习课程,共2个课程,价值600元!</p>', '2', '凃洁,黎宁,林斌,孟卫东,郑克强,林祖荣,田佩淮', 'g2', '理科,2300,3300/文科,1500,2100', 'edm.jpg', 'service2_img.png', '', '2012-05-16 02:00:00', '2012-05-30 03:15:00', '0', '0000-00-00 00:00:00', '0', '2012-05-16 07:06:00', '0', '0', ''
'P0001', 's187927,s184480', '内部测试打包课', '内部测试打包课请勿发布', '0', '内部测试打包课', '0', ',,/,,/', 'banner_register_20170911.jpg', 'banner_register_20170911.jpg', 'banner_register_20170911.jpg', '0000-00-00 00:00:00', '1970-00-00 00:00:00', '0', '0000-00-00 00:00:00', '0', '2018-01-18 08:56:10', '0', '0', ''
'p1', 'p1', '图片入口', '测试测试', '1', '凃洁,黎宁,林斌,孟卫东,郑克强,林祖荣,田佩淮', 'g1', '优惠价,2300,3300', '20141114huyhi.jpg', '2014020807.png', '', '2012-05-09 00:00:00', '2021-08-31 00:00:00', '1', '0000-00-00 00:00:00', '0', '2012-05-09 06:48:25', '0', '0', ''
* LineProcessor.php
<?php
class LineProcessor {
/** @var \SplFileObject */
private $file;
/** @var int */
private $linum;
/** @var callable */
protected $handler;
public function __construct(string $path) {
$this->file = new \SplFileObject($path, 'r');
$this->linum = 0;
}
public function getline() /* :string */ {
return $this->file->fgets();
}
/**
* 试着读取$limit行, 对每一行执行$callback
* @param callable $callback
* @param int $limit default unlimited
* @return int 实际读取的行数
*/
public function forEach(callable $callback, int $limit = 0) {
// unlimited
if (0===$limit) {
while ($this->file->valid()) {
$line = $this->file->fgets();
call_user_func($callback, $line, $this->linum);
$this->linum++;
}
return 0;
}
for ($i = 0; $i < $limit && $this->file->valid(); $this->linum++) {
$line = $this->file->fgets();
if (!empty($line)) {
call_user_func($callback, $line, $i);
$i++;
}
}
return $i;
}
public function setHandler(callable $cb) {
if (!$cb) {
$cb = function($cur, $i) {
printf("%d %s\n", $i, $cur);
};
}
$this->handler = $cb;
}
public function run() {
$this->forEach($this->handler);
}
}
* index.php
<?php
function __autoload($className) {
include $className.'.php';
}
if ($argc < 2) {
echo 'Usage: php '.__FILE__.' input.txt > output.sql'.PHP_EOL;
exit;
}
$p = new \LineProcessor($argv[1]);
$a = explode(DIRECTORY_SEPARATOR, $argv[1]);
$filename = array_pop($a);
$parts = explode('.', $filename);
$tableName = $parts[0];
$fs = $p->getline();
$fs = substr($fs, 1);
$fs[strlen($fs)-1] = ' ';
$fields = explode(', ', $fs);
foreach ($fields as &$field) {
$field = trim($field);
}
$fs = '`'.implode('`,`', $fields).'`';
$sql = "insert into `$tableName`(".$fs.") values";
$p->setHandler(function($cur) use ($sql) {
// 去掉结尾\n, window \r\n 需要-2
$cur = substr($cur, 0, strlen($cur)-1);
if (!empty($cur)) {
echo $sql.'('.$cur.');'.PHP_EOL;
}
});
$p->run();
* 导出建表语句 创建表
show create table W_SalesPackages;
copy filed (unquoted)
命令行连接mysql 粘贴进去创建表 (先创建数据库)
mysql -uroot -hlocalhost -pXXXX--database XXXDB
* 生成sql插入语句
php ~/utility/lineprocessor/index.php ~/data/W_SalesPackages.txt > ~/data/W_SalesPackages.sql
W_SalesPackages.txt 文件名与表名 W_SalesPackages 一致
* 导入数据
(mysql命令行中)>
source ~/data/W_SalesPackages.sql