$fileName = 文件名,须要有权限加载到$tableName = 表名$flag = 文件中,每个值之间分分隔符39;\n' 换行符$keysStr = 数据库中表字段须要和插入得数据逐一对应如果指定了REPLACE,主键冲突时,新插入的行会替代原有行"load data concurrent infile '{$fileName}' REPLACE into table {$tableName} FIELDS TERMINATED BY '" . $flag . "' lines terminated by '\n' ({$keysStr});"
PHP代码如下(此处利用了TP框架)
$params['type'] = Env::get('database.type', 'mysql'); $params['hostname'] = Env::get('database.hostname', ''); $params['database'] = Env::get('database.database', ''); $params['username'] = Env::get('database.rootuser', 'root'); $params['password'] = Env::get('database.rootpass', '123456'); $params['hostport'] = Env::get('database.hostport', ''); $params['prefix'] = Env::get('database.prefix', ''); $params['params'] = [\PDO::ATTR_CASE => \PDO::CASE_LOWER, \PDO::ATTR_EMULATE_PREPARES => true, \PDO::MYSQL_ATTR_LOCAL_INFILE => true]; $re = Db::connect($params)->execute($executeSql);
实行
有可能涌现的缺点
涌现报错SQLSTATE[HY000]: General error: 1290 The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
办理方法
在数据库得配置文件中找到[mysqld] 添加secure_file_priv = ''
重启数据库即可