Home Database Mysql Tutorial How to import formatted data into MySQL

How to import formatted data into MySQL

Mar 28, 2019 am 11:49 AM
mysql Import Data

How to import formatted data in MySQL? This article will introduce to you the method of importing formatted data into MySQL. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

In some scenarios, we need to import a batch of data in a specific format into the mysql database. There are many ways to do it. You can use shell scripts or python. Today we will introduce two more convenient commands, mysqlimport and load data

mysqlimport

Usage method

mysqlimport [options] db_name textfile1 [textfile2 ...]
# db_name 对应数据库名称
# textfile 文件名,对应要插入的数据库表名
# 比如:mysqlimport a8 campaign.txt 会把campaign.txt中的数据插入到a8数据库中的campaign表
Copy after login

Note: The campaign.txt file must be in the /usr/local/mysql/var/a8/ directory, which is the directory where the database is located.

If it is not in the corresponding directory, the following error will be returned:

mysqlimport -uroot -p123456 -hlocalhost -P3306 a8 campaign.txt 
/usr/local/mysql/bin/mysqlimport: Error: File '/usr/local/mysql/var/a8/campaign.txt' not found (Errcode: 2), when using table: campaign
Copy after login

Common options

--columns=id,name,creator...
# 对应的数据表列名,定义被导入文件中的每一列对应的数据库表列名

--fields-terminated-by=
# 文件字段以什么分隔,参数为字符串,默认为\t

--fields-enclosed-by=
# 数据域用什么符号扩起来,默认为空,一般可以是双引号、括号等

--fields-optionally-enclosed-by=
# 数据域可以用什么符号括起来,因为为只有部分数据用这些符号括起来

--fields-escaped-by=
# 转义字符,参数为字符,默认为\

--lines-terminated-by=
# 数据行以什么结束,参数为字符串,windows默认为\r\n

--user=user_name 或 -u user_name
--password=[password] 或 -p[password]
--host=host_name 或 -h hostname
--port=port_num,或 -P port_num
# 定义用户名、密码、mysql服务器地址和用于连接的TCP/IP端口号,默认为mysql默认端口3306

--ignore-lines=n
# 忽视数据文件的前n行,因为很多数据文件前面有表头

--delete -D
# 在把文件中的数据插入前删除表中原先的数据

--local -L
# 指定从客户端电脑读入数据文件,否则从服务器电脑读取

--lock-tables -l
# 处理文本文件前锁定所有表以便写入,确保所有表在服务器上保持同步

--protocol={TCP | SOCKET | PIPE | MEMORY}
使用的连接协议

--force -f
#忽视错误。例如,如果某个文本文件的表不存在,继续处理其它文件,不使用--force,如果表不存在则mysqlimport退出

--compress -C
# 压缩在客户端和服务器之间发送的所有信息(如果二者均支持压缩)

--silent,-s
# 沉默模式,只有出现错误时才输出

--socket=path,-S path
# 当连接localhost时使用的套接字文件(为默认主机)

--verbose,-v
# 冗长模式。打印出程序操作的详细信息。

--version,-V
# 显示版本信息并退出。
Copy after login

load data

Usage method

mysql> load data [low_priority] [local] infile 'file_name txt' [replace | ignore]
into table tbl_name
[fields]
[terminated by '\t']
[OPTIONALLY] enclosed by '']
[escaped by '\' ]]
[lines terminated by 'n']
[ignore number lines]
[(id,name,creator)]
Copy after login

Instructions:

## The #load data infile statement imports text data into the data table. Before using this command, the mysqld process (service) must be running. Please make sure you have read permission on the file before using it

1. If you specify the keyword low_priority, MySQL will wait until no one else reads the table before inserting data. You can use the following command:

mysql> load data low_priority infile "/home/root/data.sql" into table campaign;
Copy after login
2. If you specify the local keyword, it means reading the file from the client host. If local is not specified, the file must be located on the server.

3. The replace and ignore keywords control the duplicate processing of existing unique key records. If you specify replace, the new row will replace the existing row with the same unique key value. If you specify ignore, the input of duplicate rows for existing rows with unique keys is skipped. If you do not specify either option, an error occurs when a duplicate key is found, and the remainder of the text file is ignored. For example:

mysql> load data low_priority infile "/home/root/data.sql" replace into table campaign;
Copy after login
4, delimiter

1) The fields keyword specifies the splitting format of file fields. If this keyword is used, the MySQL parser hopes to see at least one of the following Options:

terminated by:分隔符,字段是以什么字符作为分隔符
enclosed by:字段括起字符,例:` "周丽","10","学习很好" ` 这样的一行,就需要这么写 ` ENCLOSED BY '"' `
escaped by:转义字符
lines terminated by:描述字段的分隔符,默认情况下是tab字符(\t) 
ignore number lines:用来忽略导入文件的开始的行。例如:number=1,则忽略导入文件的第一行数据。
Copy after login
For example:

mysql> load data infile "/home/root/data.sql" replace into table campaign fields terminated by',' enclosed by '"';
Copy after login
2) The lines keyword specifies the delimiter for each record. The default is 'n', which is the newline character

If both fields are specified The fields must come before lines. If you do not specify the fields keyword, the default value is the same as writing:
fields terminated by'\t' enclosed by ' '' ' escaped by'\\'If you do not specify a lines Clause, the default value is the same as this:
lines terminated by'\n' For example:

mysql> load data infile "/root/load.txt" replace into table test fields terminated by ',' lines terminated by '/n';
Copy after login
5, load data infile can load the file according to the specified column Import into database. When we want to import part of the data, we need to add some columns (columns/fields/fields) to the MySQL database to meet some additional needs. For example, when we want to upgrade from an Access database to a MySQL database

The following example shows how to import data into a specified column (field):

mysql> load data infile "/home/root/campaign.txt" into table campaign(id, name, creator);
Copy after login
6. When looking for files on the server host , the server uses the following rules:

● If an absolute path name is given, the server uses that path name.

● If a relative pathname with one or more preceding components is given, the server searches for the file relative to the server's data directory.

● If a file name without a prefix is ​​given, the server looks for the file in the database directory of the current database.

For example: /campaign.txt is read from the server's data directory, while campaign.txt is read from the database directory of the current database.

Recommended video tutorials: "

MySQL Tutorial"

The above is the entire content of this article, I hope it will be helpful to everyone's learning. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of How to import formatted data into MySQL. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP's big data structure processing skills PHP's big data structure processing skills May 08, 2024 am 10:24 AM

Big data structure processing skills: Chunking: Break down the data set and process it in chunks to reduce memory consumption. Generator: Generate data items one by one without loading the entire data set, suitable for unlimited data sets. Streaming: Read files or query results line by line, suitable for large files or remote data. External storage: For very large data sets, store the data in a database or NoSQL.

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values ​​to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

How to create a MySQL table using PHP? How to create a MySQL table using PHP? Jun 04, 2024 pm 01:57 PM

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

The difference between oracle database and mysql The difference between oracle database and mysql May 10, 2024 am 01:54 AM

Oracle database and MySQL are both databases based on the relational model, but Oracle is superior in terms of compatibility, scalability, data types and security; while MySQL focuses on speed and flexibility and is more suitable for small to medium-sized data sets. . ① Oracle provides a wide range of data types, ② provides advanced security features, ③ is suitable for enterprise-level applications; ① MySQL supports NoSQL data types, ② has fewer security measures, and ③ is suitable for small to medium-sized applications.

See all articles