Created
January 6, 2023 09:14
-
-
Save acosonic/d6a0a254295b93486d52924a94f57d01 to your computer and use it in GitHub Desktop.
Importing all csv files to mysql via nodejs from subfolder csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Assuming your sql has this global var set or you can set it via additional query: | |
// set global local_infile=true; | |
const mysql = require('mysql'); // or use import if you use TS | |
const util = require('util'); | |
const fs = require("fs"); | |
const conn = mysql.createConnection({ | |
host: "localhost", | |
user: "root", | |
password: "pw", | |
database: "database" | |
}); | |
// node native promisify | |
const query = util.promisify(conn.query).bind(conn); | |
async function loadf(filename) { | |
try { | |
const rows = await query('LOAD DATA LOW_PRIORITY LOCAL INFILE \''+filename+'\' REPLACE INTO TABLE `tablename` CHARACTER SET latin1 FIELDS TERMINATED BY \',\' OPTIONALLY ENCLOSED BY \'"\' ESCAPED BY \'"\' LINES TERMINATED BY \'\\r\\n\' IGNORE 1 LINES (`Field1`, `Field2`, `Field3`); '); | |
console.log(rows); | |
} finally { | |
console.log("done: "+filename); | |
} | |
} | |
const filelist = fs.readdirSync('./csv/'); | |
for(var i = 0; i< filelist.length;i++) { | |
loadf('./csv/'+filelist[i]); | |
//console.log(filelist[i]); | |
} | |
// conn.end(); //this is commented on purpose | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment