1655995740
在本教程中,您将了解如何使用 Node js 和 Fast CSV 包将 CSV 文件导入 MySQL 数据库。
Multer 包使将文件上传到数据库变得容易。Multer 是一个流行的 npm 包,它使文件上传变得不那么痛苦。
它是为 node js 制作的,这是一个强大的中间件,可以处理多部分/表单数据,主要用于上传文件。
我们将结合 multer 和 Fast-csv 模块将文件导入 MySQL 数据库。
Fast CSV 是一个方便的包,可以在 node js 环境中解析和格式化 CSV 或任何其他分隔值文件。
我们还将在本指南中使用其他模块;这些包将帮助我们在 Node js 中创建 csv 文件导入功能。
进入终端的命令提示符并运行给定的命令以创建节点项目的空文件夹。
mkdir node-csv
进入空文件夹:
cd node-csv
键入建议的命令,然后运行给定的命令以生成新的package.json文件。
npm init
现在,节点脚本已经创建,它需要注册到给定的脚本部分,确保在package.json文件中添加脚本名称。
{
"main": "app.js",
}
Install NPM Modules
此外,我们需要在终端上输入给定的命令并按回车键来安装给定的模块。
npm install fast-csv express mysql multer nodemon body-parser
创建用于存储 CSV 文件记录的新表很容易,您可以使用给定的 SQL 命令创建 MySQL 数据表。
CREATE TABLE `users` (
`id` bigint(11) NOT NULL,
`name` varchar(150) DEFAULT NULL,
`email` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE= InnoDB DEFAULT CHARSET=utf8
让我们创建一个index.html文件,该文件将处理功能的视图。
我们必须在 head 部分添加 Bootstrap 5 链接,这将允许我们创建文件上传 UI 组件。
<!DOCTYPE html>
<html lang="en">
<head>
<title>Node CSV File Upload</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css"
rel="stylesheet"
/>
</head>
<body>
<h2 class="mb-3">Node Import CSV File to MySQL database Example</h2>
<form action="/import-csv" method="post" enctype="multipart/form-data">
<div class="mb-3">
<input
type="file"
class="form-control"
name="import-csv"
accept="csv"
/>
</div>
<div class="d-grid">
<input type="submit" class="btn btn-dark" value="Store File" />
</div>
</form>
</body>
</html>
打开app.js文件,在此文件中,您必须逐行放置给定的代码。
在这个文件中,我们正在建立与 MySQL 的数据库连接,创建将处理 csv 文件上传到数据库的路由并设置节点应用程序端口。
const express = require('express')
const bodyparser = require('body-parser')
const fs = require('fs');
const path = require('path')
const mysql = require('mysql')
const multer = require('multer')
const csv = require('fast-csv');
const app = express()
app.use(express.static("./public"))
app.use(bodyparser.json())
app.use(bodyparser.urlencoded({
extended: true
}))
// Database connection
const db = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "test"
})
db.connect(function (err) {
if (err) {
return console.error(err.message);
}
console.log('Connected to database.');
})
var storage = multer.diskStorage({
destination: (req, file, callBack) => {
callBack(null, './uploads/')
},
filename: (req, file, callBack) => {
callBack(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname))
}
})
var upload = multer({
storage: storage
});
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
app.post('/import-csv', upload.single("import-csv"), (req, res) =>{
uploadCsv(__dirname + '/uploads/' + req.file.filename);
console.log('File has imported :' + err);
});
function uploadCsv(uriFile){
let stream = fs.createReadStream(uriFile);
let csvDataColl = [];
let fileStream = csv
.parse()
.on("data", function (data) {
csvDataColl.push(data);
})
.on("end", function () {
csvDataColl.shift();
db.connect((error) => {
if (error) {
console.error(error);
} else {
let query = 'INSERT INTO users (id, name, email) VALUES ?';
db.query(query, [csvDataColl], (error, res) => {
console.log(error || res);
});
}
});
fs.unlinkSync(uriFile)
});
stream.pipe(fileStream);
}
const PORT = process.env.PORT || 5555
app.listen(PORT, () => console.log(`Node app serving on port: ${PORT}
这是将启动节点应用程序的命令,即使您对节点应用程序的文件进行了微小的更改,nodemon 模块也会重新启动您的节点服务器。
nodemon
您可以在浏览器上键入给定的 URL 来测试应用程序。
http://localhost:5555
Node js 是一个很棒的框架,它允许开发人员为他们的 Web 应用程序构建他们想要的任何功能。
感谢 npm 社区,它提供了构建所需功能所需的几乎所有类型的模块。
在本教程中,我们学习了如何使用 Fast CSV、Multer、MySQL 以及最重要的 express js 将 CSV 文件上传到 MySQL 数据库。
来源:https ://www.positronx.io/node-import-csv-file-data-to-mysql-database-with-html-form/
1655995740
在本教程中,您将了解如何使用 Node js 和 Fast CSV 包将 CSV 文件导入 MySQL 数据库。
Multer 包使将文件上传到数据库变得容易。Multer 是一个流行的 npm 包,它使文件上传变得不那么痛苦。
它是为 node js 制作的,这是一个强大的中间件,可以处理多部分/表单数据,主要用于上传文件。
我们将结合 multer 和 Fast-csv 模块将文件导入 MySQL 数据库。
Fast CSV 是一个方便的包,可以在 node js 环境中解析和格式化 CSV 或任何其他分隔值文件。
我们还将在本指南中使用其他模块;这些包将帮助我们在 Node js 中创建 csv 文件导入功能。
进入终端的命令提示符并运行给定的命令以创建节点项目的空文件夹。
mkdir node-csv
进入空文件夹:
cd node-csv
键入建议的命令,然后运行给定的命令以生成新的package.json文件。
npm init
现在,节点脚本已经创建,它需要注册到给定的脚本部分,确保在package.json文件中添加脚本名称。
{
"main": "app.js",
}
Install NPM Modules
此外,我们需要在终端上输入给定的命令并按回车键来安装给定的模块。
npm install fast-csv express mysql multer nodemon body-parser
创建用于存储 CSV 文件记录的新表很容易,您可以使用给定的 SQL 命令创建 MySQL 数据表。
CREATE TABLE `users` (
`id` bigint(11) NOT NULL,
`name` varchar(150) DEFAULT NULL,
`email` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE= InnoDB DEFAULT CHARSET=utf8
让我们创建一个index.html文件,该文件将处理功能的视图。
我们必须在 head 部分添加 Bootstrap 5 链接,这将允许我们创建文件上传 UI 组件。
<!DOCTYPE html>
<html lang="en">
<head>
<title>Node CSV File Upload</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css"
rel="stylesheet"
/>
</head>
<body>
<h2 class="mb-3">Node Import CSV File to MySQL database Example</h2>
<form action="/import-csv" method="post" enctype="multipart/form-data">
<div class="mb-3">
<input
type="file"
class="form-control"
name="import-csv"
accept="csv"
/>
</div>
<div class="d-grid">
<input type="submit" class="btn btn-dark" value="Store File" />
</div>
</form>
</body>
</html>
打开app.js文件,在此文件中,您必须逐行放置给定的代码。
在这个文件中,我们正在建立与 MySQL 的数据库连接,创建将处理 csv 文件上传到数据库的路由并设置节点应用程序端口。
const express = require('express')
const bodyparser = require('body-parser')
const fs = require('fs');
const path = require('path')
const mysql = require('mysql')
const multer = require('multer')
const csv = require('fast-csv');
const app = express()
app.use(express.static("./public"))
app.use(bodyparser.json())
app.use(bodyparser.urlencoded({
extended: true
}))
// Database connection
const db = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "test"
})
db.connect(function (err) {
if (err) {
return console.error(err.message);
}
console.log('Connected to database.');
})
var storage = multer.diskStorage({
destination: (req, file, callBack) => {
callBack(null, './uploads/')
},
filename: (req, file, callBack) => {
callBack(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname))
}
})
var upload = multer({
storage: storage
});
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
app.post('/import-csv', upload.single("import-csv"), (req, res) =>{
uploadCsv(__dirname + '/uploads/' + req.file.filename);
console.log('File has imported :' + err);
});
function uploadCsv(uriFile){
let stream = fs.createReadStream(uriFile);
let csvDataColl = [];
let fileStream = csv
.parse()
.on("data", function (data) {
csvDataColl.push(data);
})
.on("end", function () {
csvDataColl.shift();
db.connect((error) => {
if (error) {
console.error(error);
} else {
let query = 'INSERT INTO users (id, name, email) VALUES ?';
db.query(query, [csvDataColl], (error, res) => {
console.log(error || res);
});
}
});
fs.unlinkSync(uriFile)
});
stream.pipe(fileStream);
}
const PORT = process.env.PORT || 5555
app.listen(PORT, () => console.log(`Node app serving on port: ${PORT}
这是将启动节点应用程序的命令,即使您对节点应用程序的文件进行了微小的更改,nodemon 模块也会重新启动您的节点服务器。
nodemon
您可以在浏览器上键入给定的 URL 来测试应用程序。
http://localhost:5555
Node js 是一个很棒的框架,它允许开发人员为他们的 Web 应用程序构建他们想要的任何功能。
感谢 npm 社区,它提供了构建所需功能所需的几乎所有类型的模块。
在本教程中,我们学习了如何使用 Fast CSV、Multer、MySQL 以及最重要的 express js 将 CSV 文件上传到 MySQL 数据库。
来源:https ://www.positronx.io/node-import-csv-file-data-to-mysql-database-with-html-form/
1595905879
HTML to Markdown
MySQL is the all-time number one open source database in the world, and a staple in RDBMS space. DigitalOcean is quickly building its reputation as the developers cloud by providing an affordable, flexible and easy to use cloud platform for developers to work with. MySQL on DigitalOcean is a natural fit, but what’s the best way to deploy your cloud database? In this post, we are going to compare the top two providers, DigitalOcean Managed Databases for MySQL vs. ScaleGrid MySQL hosting on DigitalOcean.
At a glance – TLDR
ScaleGrid Blog - At a glance overview - 1st pointCompare Throughput
ScaleGrid averages almost 40% higher throughput over DigitalOcean for MySQL, with up to 46% higher throughput in write-intensive workloads. Read now
ScaleGrid Blog - At a glance overview - 2nd pointCompare Latency
On average, ScaleGrid achieves almost 30% lower latency over DigitalOcean for the same deployment configurations. Read now
ScaleGrid Blog - At a glance overview - 3rd pointCompare Pricing
ScaleGrid provides 30% more storage on average vs. DigitalOcean for MySQL at the same affordable price. Read now
MySQL DigitalOcean Performance Benchmark
In this benchmark, we compare equivalent plan sizes between ScaleGrid MySQL on DigitalOcean and DigitalOcean Managed Databases for MySQL. We are going to use a common, popular plan size using the below configurations for this performance benchmark:
Comparison Overview
ScaleGridDigitalOceanInstance TypeMedium: 4 vCPUsMedium: 4 vCPUsMySQL Version8.0.208.0.20RAM8GB8GBSSD140GB115GBDeployment TypeStandaloneStandaloneRegionSF03SF03SupportIncludedBusiness-level support included with account sizes over $500/monthMonthly Price$120$120
As you can see above, ScaleGrid and DigitalOcean offer the same plan configurations across this plan size, apart from SSD where ScaleGrid provides over 20% more storage for the same price.
To ensure the most accurate results in our performance tests, we run the benchmark four times for each comparison to find the average performance across throughput and latency over read-intensive workloads, balanced workloads, and write-intensive workloads.
Throughput
In this benchmark, we measure MySQL throughput in terms of queries per second (QPS) to measure our query efficiency. To quickly summarize the results, we display read-intensive, write-intensive and balanced workload averages below for 150 threads for ScaleGrid vs. DigitalOcean MySQL:
ScaleGrid MySQL vs DigitalOcean Managed Databases - Throughput Performance Graph
For the common 150 thread comparison, ScaleGrid averages almost 40% higher throughput over DigitalOcean for MySQL, with up to 46% higher throughput in write-intensive workloads.
#cloud #database #developer #digital ocean #mysql #performance #scalegrid #95th percentile latency #balanced workloads #developers cloud #digitalocean droplet #digitalocean managed databases #digitalocean performance #digitalocean pricing #higher throughput #latency benchmark #lower latency #mysql benchmark setup #mysql client threads #mysql configuration #mysql digitalocean #mysql latency #mysql on digitalocean #mysql throughput #performance benchmark #queries per second #read-intensive #scalegrid mysql #scalegrid vs. digitalocean #throughput benchmark #write-intensive
https://loizenai.com Programming Tutorial
1621169305
Nodejs Express RestAPI – Upload/Import CSV File to MySQL – using Fast-CSV & Multer
In the tutorial, Grokonez shows how to upload & import CSV File/Data to MySQL using fast-csv
and multer
libs.
Related post:
CSV File ->
id,name,address,age
1,Jack Smith,Massachusetts,23
2,Adam Johnson,New York,27
3,Katherin Carter,Washington DC,26
4,Jack London,Nevada,33
5,Jason Bourne,California,36
-> Results:
package.json
file by cmd: npm init
-> Then install express
, mysql
, fast-csv
& multer
libs:
More at:
Nodejs Express RestAPI – Upload/Import CSV File to MySQL – using Fast-CSV & Multer
#nodejs #express #mysql #fast-csv #multer
1597736283
Looking to build dynamic, extensively featured, and full-fledged web applications?
Hire NodeJs Developer to create a real-time, faster, and scalable application to accelerate your business. At HourlyDeveloper.io, we have a team of expert Node.JS developers, who have experience in working with Bootstrap, HTML5, & CSS, and also hold the knowledge of the most advanced frameworks and platforms.
Contact our experts: https://bit.ly/3hUdppS
#hire nodejs developer #nodejs developer #nodejs development company #nodejs development services #nodejs development #nodejs
https://loizenai.com Programming Tutorial
1620655032
CSV File/Data – Nodejs Express RestAPI – Download/Extract CSV Data/File from MySQL
In the tutorial, I shows how to download & extract CSV File/Data from MySQL with Nodejs Express and json2csv
lib.
What will we do?
json2csv
lib to save data from json to CSV file.Related posts:
json2csv
: Converts json
into csv
with column titles and proper line endings.– MySQL Data:
Results:
package.json
file by cmd: npm init
-> Then install express
, mysql
& json2csv
libs:
More at:
CSV File/Data – Nodejs Express RestAPI – Download/Extract CSV Data/File from MySQL
#csv #nodejs #express #restapi #mysql