How to import formatted data into MySQL
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表
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
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 # 显示版本信息并退出。
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)]
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 it1. 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;
mysql> load data low_priority infile "/home/root/data.sql" replace into table campaign;
terminated by:分隔符,字段是以什么字符作为分隔符 enclosed by:字段括起字符,例:` "周丽","10","学习很好" ` 这样的一行,就需要这么写 ` ENCLOSED BY '"' ` escaped by:转义字符 lines terminated by:描述字段的分隔符,默认情况下是tab字符(\t) ignore number lines:用来忽略导入文件的开始的行。例如:number=1,则忽略导入文件的第一行数据。
mysql> load data infile "/home/root/data.sql" replace into table campaign fields terminated by',' enclosed by '"';
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';
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);
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.
